C#如何通过单击行在DataGridView中选择数据 [英] C# How to Select data in DataGridView by clicking on a row
问题描述
我使用以下代码将DataSet中的数据导入DataGridView
I am using the following code to get data from a DataSet into a DataGridView
数据加载到DataGridView就好了。
The data loads into the DataGridView just fine.
现在我希望用户能够连续选择任何"单元格"并选择该数据。
Now I want the user to be able to select any "Cell" in a row and select that data.
右现在唯一可行的方法是用户选择FIRST 行中的单元格。
Right now the only way it works is if the user selects the FIRST cell in the row.
私有
void decomCstdgv_CellClick( object
sender, DataGridViewCellEventArgs e)
{
foreach(decomCstdgv.SelectedRows中的DataGridViewRow行)
{
sF_Name = row.Cells [0] .Value.ToString( );
sL_Name = row.Cells [1] .Value.ToString( );
sAddress = row.Cells [4] .Value.ToString( );
}
)
任何人
请帮助。
谢谢Jay
推荐答案
尝试这种方法
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
{
return;
}
sf_name = dataGridView.CurrentRow.Cells[0].Value.ToString;
sl_name = dataGridView.CurrentRow.Cells[1].Value.ToString();
sAddress = dataGridView.CurrentRow.Cells[4].Value.ToString();
}
希望它可以帮到你
关心Rinaldo
这篇关于C#如何通过单击行在DataGridView中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!