C# Windows 窗体应用程序中的 DataGridView

本文关键字:DataGridView 应用程序 Windows 窗体 | 更新日期: 2023-09-27 18:30:31

我正在使用SQL Server 2005在C#WindowsForms应用程序中处理DataGridView,我在我的DataGridView中添加了1列,即"名称",并通过选择行到文本框来显示它,但它只显示一列,即"名称"到文本框。但是我还有其他列,例如城市状态,我没有添加到我的DataGridView有没有办法通过单击从DataGridViewTextboxes的单列"名称"来显示所有数据库记录?

private void dataGridView1_CellClick (Object Sender, DataGridViewCellEventArgs e){ i = e.RowIndex; DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

        textBox1.Text = row.Cells[0].Value.ToString();
        String str = "server=PC2-PC;database=demo;Integrated Security=true";
        SqlConnection con = new SqlConnection(str);
        con.Open();
           if (e.RowIndex != -1)
        {
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[Name].Value == null ? "" : dataGridView1.Rows[e.RowIndex].Cells[Name].Value.ToString();
            CallData();
        }
    }

C# Windows 窗体应用程序中的 DataGridView

使用解决方案,可以在 DataGridView 中生成其他列,并将"可见"更改为 false。 这样,您可以在填写"名称列"时填充它们,并在用户单击行并显示在文本框中使用其数据。

但是这种在表单事件中使用数据库的方式不是标准代码。