WPF Datagrid read a cell value

前端 未结 3 1735
花落未央
花落未央 2020-12-22 01:07

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].ToStr         


        
3条回答
  •  醉话见心
    2020-12-22 02:02

    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.

提交回复
热议问题