I'm searching a way for making a binding of a datagrid colomn on a row of a list
looking for something like that in pseudo code:
DataGridTextColumn colBuild = new DataGridTextColumn
{
Header = "Custom Column Name",
Binding = new Binding("myList.elementAt(0)")
};
I known this can looks strange but I'm looking for adding a variable number of custom columns on a datagrid in addition to fixed ones. And I don't know how to get data filled in those columns.
Can someone help me?
Related
My question is "Is there a way to use a CheckBox inside a fixed row cell in a C1.Win.C1FlexGrid?".
I have a C1FlexGrid with two Fixed rows. (It is important to mention here that I am using the C1.Win.C1FlexGrid grid and not the WPF, or SilverLight version)
The first fixed row I have is used for the headers as usual. the second one though is customized to perform some other tasks all is working fine except for one task I can't get done. I need to use a CheckBox inside one cell of the cells of the second Fixed row (just like any Boolean cell in the grid's normal rows) because I want to use this CheckBox to Check/Uncheck all check boxes in the same column.
Of course setting the column datatype to bool won't do the job for fixed rows. Setting the editor of the cell to a CheckBox won't do also as the editor won't be visible at all times but only when cell is selected. Also, based on my research, there is a CellFactory property that some threads are discussing which can be used to do this job, but CellFactory is not implemented in C1.Win.C1FlexGrid class but only in WPF, SilverLight and Phone versions of the grid.
Any ideas on how to do this?
Create a new CellStyle with a Boolean DataType and set it to any Cell that you need. Here’s the code to implement it, assuming the cell is in Row 1 and Column 1:
//Implement 2 fixed rows
c1FlexGrid1.Rows.Fixed = 2;
//create and set a new style to the reqd. cell
var cs = c1FlexGrid1.Styles.Add("Boolean");
//set DataType
cs.DataType = typeof(Boolean);
//Set any alignment
cs.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.CenterCenter;
c1FlexGrid1.SetCellStyle(1, 1, cs);
Thanks,
Richa
Okay, this is driving me nuts. I've spent hours trying to figure out what should be a simple solution, but I'm having no luck.
I have a [WPF Toolkit] DataGrid on an XAML page that has a DataTable as its ItemsSource. I also have a button on my page that gets the DataGrid's SelectedIndex (selected row) and uses it as a variable in a function that reads the bound DataTable's row at that index and returns a value. Everything works fine until I click a column header to sort it. It sorts the DataGrid but does not sort the DataTable with it, so my SelectedIndex has changed but the index of the DataTable has not, thus it returns the wrong value.
I've looked for Column Header click events - no luck; I tried to get the header of the column by which the grid is currently sorted - nada; I tried to use a "Click" EventSetter on the DataGridTextColumn template - not supported.
I'm completely at a loss. If WPF is supposed to be an improvement over Windows Forms, why has some of the simple functionality been removed? (It's also dumb that you have to bind data just to add rows, just saying.) I can use a Windows Forms DataGrid and won't have any trouble, but then I can't style it.
Maybe I'm not performing the check properly or something...? Below is my retrieval/output code. Anyone have any ideas??? Your help will be greatly appreciated!
DataRow selectedRow = my_data.Tables[0].Rows[my_grid.SelectedIndex];
MessageBox.Show(selectedRow["ItemName"]);
The sorting is applied to the DefaultView of the datatable.So it will not be applied to the Datatable directly.To access the sorted table use
DataTable.DefaultView.ToTable()
I am trying to find out how to read the value of my WPF datagrid cells.
something along the lines of
String myString = myDataGrid.Cells[1][2].ToString();
the datagrid has been created in XAML and i have populated the datagrid with row data by using
reportGrid.Items.Add(new cbResultRow() { ... });
now I want to go back and read cell values in my datagrid.
I have seen some examples of reading data from a selected row or cell, by I don't have any selection (the user doesnt interact with the datagrid).
i have also seen code like
foreach(DataGridRow myrow in myDataGrid.Rows)
however the compiler says Rows is not a member of datagrid.
I have searched for several hours to try to find out how to do what I would have thought was a very simple thing!
please help,
Thanks,
will.
The WPF datagrid was built to bind to something like a DataTable. The majority of the time, you will modify the DataTable, and the Rows/Columns within the DataTable that is bound to the DataGrid.
The DataGrid itself is the Visual Element for the DataTable. Here is a pretty good tutorial on how to do this. To modify data within a loop would look something like this.
foreach(DataRow row in myTable.Rows)
{
row["ColumnTitle"] = 1;
}
This would simply make all the values in Column "ColumnTitle" equal to 1. To access a single cell it would look something like this.
myTable.Rows[0][0] = 1;
This would set the first cell in your DataTable to 1.
This might help someone else.
foreach (DataRowView row in dgLista.SelectedItems)
{
string text = row.Row.ItemArray[index].ToString();
}
Good luck!
Here's a summary of the solution.
Winform
Type: System.windows.Forms.DataGridView
// C#
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//"Column1" = column name in DataGridView
string text = row.Cells["Column1"].value.ToString();
}
WPF equivalent
Type: DataGrid
// C#
foreach (DataRowView row in dataGrid.Items)
{
string text = row.Row.ItemArray[index].ToString();
}
I have built a dynamic gridview using the following code
grdVariants.Columns.Clear();
int i = 0;
foreach (DataColumn column in options.Columns)
{
grdVariants.Columns.Add(new GridViewColumn
{
Header = column.ColumnName,
DisplayMemberBinding = new Binding(string.Format("[{0}]", i++))
});
}
This will dynamically generate my columns at runtime, I then bind the data using
lstVariantsGrid.DataContext = options;
lstVariantsGrid.Items.Refresh();
This all works great and shows the data in the correct columns etc, the only issue i have is that I can't style the rows like I would in xaml as it is all an unknown quantity until runtime. Can anyone offer some advice on how I might go about doing this?
One of the biggest problems I have is that one of the columns needs to display the image rather than just the path which it currently shows, as well as fiddling with fonts and colors etc.
Thanks for your time.
Use a datatemplate which you can create in xaml and load in code behind and then set to the CellTemplate property of the GridViewColumn.
I'm trying to bind a JSON array to a datagrid in Silverlight 3. I do not get any exceptions but I do not see the column values in the datagrid. I do see the rows though, but I do not know what the binding property should be. I do not want to create a class, populate the class and the bind. That works. I do not know what columns and datatypes the json string contains. I want the datagrid to just show all columns that are present in the json object.
Following is the code
Dim J As JsonArray = JsonArray.Load(New StringReader("[{'name':'arun', 'age':26, good:true},{'name':'kumar', 'age':28, good:false}]"))
For Each JJ In J
MessageBox.Show(JJ("name")) 'This shows the proper names'
Next
Dim c = New DataGridTextColumn()
c.Binding = New Binding("name")
GridUsers.Columns.Add(c)
GridUsers.ItemsSource = J
I do see 2 rows in the grid, but the columns values are always blank. What am I missing the binding property?
Many Thanks,
Arun
Have you tried JSON.NET? Version 3 apparently has support specifically for Silverlight 3:
http://james.newtonking.com/archive/2008/08/25/json-net-3-0-released.aspx
This looks similar to the problem of binding to dynamically created columns. See http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/]1. The secret is to make the entire object the binding source and use a value converter and a converter parameter to identify individual members.