繁体   English   中英

C# Datagridview CellClick 事件不会将数据填充到文本框

[英]C# Datagridview CellClick event does not populate data to textboxes

所以下面的代码只有在我直接从 Datagridview 选项加载数据源时才有效。 如果我通过脚本刷新数据,它就不起作用。 我的按钮/事件触发器将调用这些函数。我们在这里的主要重点是 - 为什么在调用函数“getrefresh”后单元格单击事件不起作用。

    public void getclicked()
{
    if (dataGridView1.SelectedRows.Count > 0) // make sure user select at least 1 row 
    {
        string uid = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
        string firstn = dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
        string lastn = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
        string amount = dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
        string getDate = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;

        InputID.Text = uid;
        InputFN.Text = firstn;
        InputLastN.Text = lastn;
        InputDonate.Text = amount;
        InputDate.Text = getDate;
    }
}



    public void getrefresh()
    {
        //dataGridView1.DataSource = null;
        //dataGridView1.Refresh();

        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Chaos\OneDrive\Project\c#\Donation\Database.mdf;Integrated Security=True");
        conn.Open();

        SqlDataAdapter MyDA = new SqlDataAdapter();

        MyDA.SelectCommand = new SqlCommand("SELECT * from DonateTable", conn);

        DataTable table = new DataTable();
        MyDA.Fill(table);

        BindingSource bSource = new BindingSource();
        bSource.DataSource = table;

        dataGridView1.DataSource = bSource;
        conn.Close();

        dataGridView1.Refresh();


    }

将 DataGridView 的“SelectionMode”属性设置为“FullRowSelect”,并在 DataGridView 的“SelectionChanged”事件中调用 getclicked() 方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM