【问题标题】:textbox textchange event to click the checkbox in datagridview文本框 textchange 事件以单击 datagridview 中的复选框
【发布时间】:2019-10-15 07:34:31
【问题描述】:

我有一个文本框和一个带有复选框的数据网格视图。将excel导入datagridview。我将复选框添加到 datagridview 中。我想将数字输入到文本框中,这个数字将与 datagridview 中的列匹配。 请帮帮我!

private void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }
    private void TextBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if(e.KeyCode==Keys.Enter)
        {
            button4.Focus();
            Button4_Click(sender,e);
            textBox2.Focus();
        }
    }

    private void Button4_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {

            if (row.Cells[3].Value.ToString() == textBox2.Text)
            {
                row.Cells[0].Value = ((CheckBox)dataGridView1.Controls.Find("DataGridViewCheckBoxCell", true)[0]).Checked;
            }
        }
    }
    private void Form9_Load(object sender, EventArgs e)
    {
        TextBox textBox = new TextBox();
        textBox.TextChanged += new EventHandler(TextBox2_TextChanged);
    }

【问题讨论】:

  • 您的列是否设置为接受 Checkstate 值?

标签: c# winforms event-handling


【解决方案1】:

在设置 DataGridView 时设置此属性

dataGridView1.Columns[0].ValueType = typeof(CheckState); 

然后在您的事件处理程序中使用此设置单元格

if (row.Cells[3].Value.ToString() == textBox2.Text)
{
    row.Cells[0].Value = CheckState.Checked;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2017-05-07
    • 2023-03-13
    • 2011-11-13
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多