C#点击按钮在datagridview中查找行索引(行号)

C#点击按钮在datagridview中查找行索引(行号),c#,winforms,datagridview,row,C#,Winforms,Datagridview,Row,这是我的应用程序的基本用户界面,当我单击红色(订婚房间)按钮时,它应高亮显示datagridview中cusid=1的行,并且不应单击其他行 我无法在cusid=1的dgv中找到行索引(行号),因此我可以选择/突出显示它 这能做到吗,有什么帮助吗 我有一个bookmyroom应用程序,我使用以下代码添加了我的datagridview: OleDbConnection connection = new OleDbConnection(); connection.Open(); OleD

这是我的应用程序的基本用户界面,当我单击红色(订婚房间)按钮时,它应高亮显示datagridview中cusid=1的行,并且不应单击其他行

我无法在cusid=1的dgv中找到行索引(行号),因此我可以选择/突出显示它

这能做到吗,有什么帮助吗

我有一个bookmyroom应用程序,我使用以下代码添加了我的datagridview:

    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);

从字面上说,在我的头顶上,未经测试,你可能想尝试这样的东西,以获得下一排下来(如果你真的需要):


如果我误解了您对所添加图像的要求,请道歉。

在阅读答案之前,您应该知道,在windows应用程序中,您可以有多个表单,也可以在其他表单中执行某些任务。例如,可以将网格设置为只读,然后选择一行并单击签出按钮,然后在另一个窗体中执行编辑操作

但根据你的问题:

您可以为所有按钮分配一个事件处理程序,然后在处理程序中,对于每一行,您可以在
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;
        }

    }
}

你是一个传奇人物,谢谢,你总是能得到它,无法想象这个地方没有你。欢迎你,非常感谢你的友好回应:)顺便说一句,当你接受一个答案时,你可以通过点击答案旁边的向上箭头投赞成票。虽然你只能接受一个问题,但你可以投票选择你认为有用的答案,包括被接受的答案。
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;
        }

    }
}