How to get column value of grid in C# Windows application?

匿名 (未验证) 提交于 2019-12-03 02:53:02

问题:

How to get column value of grid in C# Windows application?

When I clicked on the cell, at that time it should get column values.

private void gridAgentDetails_Click(object sender, EventArgs e) {     for (int i = 0; i < this.gridAgentDetails.CurrentRowIndex; i++)     {         string str = gridAgentDetails.Text;     }  } 

回答1:

Use DataGridView.CurrentCell.Value:

String result = this.gridviewAgentDetails.CurrentCell.Value.ToString(); 

DataGridView.CurrentCell Property gets the currently active cell.

DataGridViewCell.Value Property gets the value associated with this cell.



回答2:

To get the Values of the Selected Cells Follow the Below Code

int CNo = datagrid.CurrentCellAddress.Y;  int StNum = (int)datagrid.Rows[CNo].Cells[0].Value;  string StName = (string)datagrid.Rows[CNo].Cells[1].Value;  int MrksSecured = (int)datagrid.Rows[CNo].Cells[2].Value; 


回答3:

try this eventHandler: [I tried with this and i got result]

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)     {         int rowIndex = e.RowIndex;         int colIndex = e.ColumnIndex;         MessageBox.Show(dataGridView1[colIndex, rowIndex].Value.ToString());     } 


回答4:

Since you comment saying you're using DataGrid, this is a WPF application, not a Form application.

WPF DataGrid Control is visual representation of data, you cannot read a specific cell directly out of a DataGrid, Hence you cannot select a specific row or column on a DataGrid, unless you bind it to a data source.

DataGrid is designed to use with DataTable. See this link to see how DataGrid is binded to DataTable.

Then, to read a specific cell value in DataGrid, you would read the DataTable instead (e.g. dataTable1[RowNumber][ColumnName] where RowNumber is an int, and ColumnName is a string.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!