WPF Datagrid read a cell value

前端 未结 3 1722
花落未央
花落未央 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 01:50

    This might help someone else.

    foreach (DataRowView row in dgLista.SelectedItems)
    {
        string text = row.Row.ItemArray[index].ToString();
    }
    

    Good luck!

    0 讨论(0)
  • 2020-12-22 01:51

    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();
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题