window 表单 TextBox 没有从 datagridview 分配任何值

window form TextBox no assign any value from datagridview

//方法

    private void txtBoxDateView(string column_name, string txt_box_value)
    {
        if (ColumnName != null && txtBoxValue != null)
        {
            ColumnName = null;
            txtBoxValue = null;
        }
        ColumnName = column_name;
        txtBoxValue = txt_box_value;
        DV = new DataView(dt);

        DV.RowFilter = string.Format(column_name + " like '%{0}%'", txt_box_value);
        dgvInventory1.DataSource = DV;
    }

/调用方法

        private void txtName_TextChanged(object sender, EventArgs e)
    {
        txtBoxDateView("ItemName", txtBrand.Text);

    }


    //select data when double click

        private void dgvInventory1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

        txtId.Text = dgvInventory1.CurrentRow.Cells[0].Value.ToString();
        txtCatg.Text = dgvInventory1.CurrentRow.Cells[1].Value.ToString();
        txtName.Text = dgvInventory1.CurrentRow.Cells[2].Value.ToString();
        //below this code doesnt assign value
        txtBrand.Text = dgvInventory1.CurrentRow.Cells[3].Value.ToString(); 
        txtLocation.Text = dgvInventory1.CurrentRow.Cells[4].Value.ToString();
        txtQty.Text = dgvInventory1.CurrentRow.Cells[5].Value.ToString(); // 
      txtqty this have another function to do

    }

当我双击该行时,该值只会分配给 txtId、txtName 和 txtCatg。我已经检查了我的 SSMS。单元格是正确的。当我调试时,即使有数据,文本框也会分配“”空值。在我前几次测试之后。 txtBoxValue = txt_box_value;每次运行时都会变空。它只会显示 txtId.text 值。并且除了 (txtqty) 之外都是 varchar。我的方法有问题吗?

我怀疑,文本更改事件会影响CurrentRow

TextChange 事件触发时,它使用过滤记录更改数据源。

克服它。在鼠标双击事件中使用下面的语句

        var dataRow = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
        if (dataRow != null)
        {
            var row = dataRow.Row;
            txtId.Text = row.ItemArray[0].ToString();
            txtCatg.Text = row.ItemArray[1].ToString();
            txtName.Text = row.ItemArray[2].ToString();
            //below this code doesnt assign value
            txtBrand.Text = row.ItemArray[3].ToString();
            txtLocation.Text = row.ItemArray[4].ToString();
            txtQty.Text = row.ItemArray[5].ToString();
            //txtqty this have another function to do
        }

上面一行不依赖视图。并使用直接 table 行数据。 txtqty 函数也可以使用 DataRow