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();
}
Related
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?
I have a dynamically created DataGridView that has a valid DataSource with one row bound to it. However, it is returning me 0 when I am doing a rowcount on the DataGridView.
dgResult.DataSource = resultDt; // a datatable containing one row
flowLayoutPanel.Controls.Add(dgResult);
int rows = dgResult.Rows.Count; // returning 0 always!
Can someone please tell me where I may be going wrong here?
I found the issue. I was displaying the grid in a tabbed page that was not selected. Unless the grid is visible, it does not raise the rowadded event (which is weird!) durnig databinding. I selected the tab page before doing the databind, and the rowcount worked.
Use this code instead:
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = resultDt;
dgResult.DataSource = bindingSource;
flowLayoutPanel.Controls.Add(dgResult);
var c = dgResult.Rows.Count;
The binding source is what's responsible for syncing your data with the control. You want to use it, rather than trying to assign the table directly to the control.
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'm trying to get a hold of all DataGridRows for a DataGrid, don't ask me why :)
The DataGrid is bound to a DataView and I'm using this code but it failes after some rows.. I guess that they haven't been created yet.
foreach (DataRowView item in datagrid.Items)
{
// Sometimes row == null...
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
// Use row...
}
Any way around this?
You can try to scroll each of the items into view before accessing them.
datagrid.ScrollIntoView(item);
I doubt it'll be very fast though if your DataGrid contains many rows
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.