如何在C# Windows应用程序中获取网格的列值?
当我单击单元格时,应该获取列值。
private void gridAgentDetails_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.gridAgentDetails.CurrentRowIndex; i++)
{
string str = gridAgentDetails.Text;
}
}
DataGridView.CurrentCell.Value
:String result = this.gridviewAgentDetails.CurrentCell.Value.ToString();
DataGridView.CurrentCell Property 获取当前活动单元格。
DataGridViewCell.Value Property 获取与此单元格关联的值。
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;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
int colIndex = e.ColumnIndex;
MessageBox.Show(dataGridView1[colIndex, rowIndex].Value.ToString());
}
由于您在评论中提到使用了DataGrid,因此这是一个WPF应用程序,而不是一个表单应用程序。
WPF DataGrid控件是数据的可视化表示,您无法直接从DataGrid中读取特定单元格,因此您无法选择DataGrid上的特定行或列,除非将其绑定到数据源。
DataGrid旨在与DataTable一起使用。请参见this link以查看如何将DataGrid绑定到DataTable。
然后,要读取DataGrid中的特定单元格值,您需要读取DataTable(例如dataTable1 [RowNumber] [ColumnName]
,其中RowNumber是int,ColumnName是字符串)。
System.Windows.Forms.DataGridView
吗? - Akram Shahda