C#在单击按钮时在datagridview中查找行索引(行号)
C# find row index (row number) in datagridview upon button click
这是我的应用程序的基本 ui,当我单击红色(订婚房间)按钮时,它应该突出显示 datagridview 中 cusid = 1 的行,并且没有其他行应该可以单击。
我无法在 cusid = 1 的 dgv 中找到行索引(行号),因此我可以 select/highlight 具体来说。
这可以做到吗,有帮助吗?
我有一个 bookmyroom 应用程序,我使用以下代码添加了我的数据网格视图:
OleDbConnection connection = new OleDbConnection();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select id,cusid,cusname,timein,
timeout,duration,amount,remark from entry";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
我的复选框列使用此代码;
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "logout";
checkColumn.HeaderText = "Logout";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10;
dataGridView1.Columns.Add(checkColumn);
从字面上看我的头顶和未经测试,你可能想尝试这样的事情来得到下一行(如果这真的是你需要的):
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
e.RowIndex >= 0)
{
var nextRowNum = senderGrid.Rows[e.RowIndex] + 1;
// do what you need with the custId cell/value
// i.e. select that row and hilite it etc
}
}
如果我误解了您对添加的图片的要求,我们深表歉意。
在您阅读答案之前,您应该知道在 windows 应用程序中您可以有多个表单,并且可以在其他表单中执行一些任务。例如,您可以将网格设置为只读,然后 select 一行并单击签出按钮并以另一种形式执行编辑操作。
但是根据你的问题:
您可以为所有按钮分配一个事件处理程序,然后在处理程序中,对于每一行,您可以查看 cusid
列(索引 = 1 的列)并检查值是否等于按钮 Text
然后激活 logout
单元格,否则将行设置为只读:
private void button_Click(object sender, EventArgs e)
{
var button = sender as Button;
foreach (DataGridViewRow item in this.dataGridView1.Rows)
{
//This is the last row, ignore it.
if (item.IsNewRow)
continue;
//Compare value of cell1 with button text
//I supposed button.Tex is the value that you want to compare with cusid
if (item.Cells[1].Value.ToString() == button.Text)
{
//Make row editable
item.ReadOnly = false;
//Select the logout cell
this.dataGridView1.CurrentCell = item.Cells[5];
}
else
{
//Make row readonly
item.ReadOnly = true;
}
}
}
这是我的应用程序的基本 ui,当我单击红色(订婚房间)按钮时,它应该突出显示 datagridview 中 cusid = 1 的行,并且没有其他行应该可以单击。
我无法在 cusid = 1 的 dgv 中找到行索引(行号),因此我可以 select/highlight 具体来说。
这可以做到吗,有帮助吗?
我有一个 bookmyroom 应用程序,我使用以下代码添加了我的数据网格视图:
OleDbConnection connection = new OleDbConnection();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select id,cusid,cusname,timein,
timeout,duration,amount,remark from entry";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
我的复选框列使用此代码;
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "logout";
checkColumn.HeaderText = "Logout";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10;
dataGridView1.Columns.Add(checkColumn);
从字面上看我的头顶和未经测试,你可能想尝试这样的事情来得到下一行(如果这真的是你需要的):
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
e.RowIndex >= 0)
{
var nextRowNum = senderGrid.Rows[e.RowIndex] + 1;
// do what you need with the custId cell/value
// i.e. select that row and hilite it etc
}
}
如果我误解了您对添加的图片的要求,我们深表歉意。
在您阅读答案之前,您应该知道在 windows 应用程序中您可以有多个表单,并且可以在其他表单中执行一些任务。例如,您可以将网格设置为只读,然后 select 一行并单击签出按钮并以另一种形式执行编辑操作。
但是根据你的问题:
您可以为所有按钮分配一个事件处理程序,然后在处理程序中,对于每一行,您可以查看 cusid
列(索引 = 1 的列)并检查值是否等于按钮 Text
然后激活 logout
单元格,否则将行设置为只读:
private void button_Click(object sender, EventArgs e)
{
var button = sender as Button;
foreach (DataGridViewRow item in this.dataGridView1.Rows)
{
//This is the last row, ignore it.
if (item.IsNewRow)
continue;
//Compare value of cell1 with button text
//I supposed button.Tex is the value that you want to compare with cusid
if (item.Cells[1].Value.ToString() == button.Text)
{
//Make row editable
item.ReadOnly = false;
//Select the logout cell
this.dataGridView1.CurrentCell = item.Cells[5];
}
else
{
//Make row readonly
item.ReadOnly = true;
}
}
}