[英]How to bind textboxes to datagridview by using textbox array
I have three buttons which loads different tables into datagridview.我有三个按钮可以将不同的表加载到 datagridview 中。 Each table has different amount of textboxes.每个表都有不同数量的文本框。 I know, that one of the ways to connect textboxes and dgv is我知道,连接文本框和 dgv 的方法之一是
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
if (dataGridView1.Columns.Count == 5)
{
textBox3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//textBox5.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//richTextBox1.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//richTextBox2.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
}
if (dataGridView1.Columns.Count == 6)
{
textBox3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//textBox5.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//textBox6.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//richTextBox1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
//richTextBox2.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
}
else
{
textBox3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
}
}
}
But its kinda long code and i want to try to make it clearer.但它的代码有点长,我想试着让它更清楚。 I want to use array of textboxes (if its even possible?), I tried this:我想使用文本框数组(如果可能的话?),我试过这个:
int i = 1;
TextBox[] textboxes = new TextBox[i];
textboxes[0] = textBox1; textboxes[1] = textBox2;
textboxes[2] = textBox2; textboxes[3] = textBox3;
for (int j = 1; i < textboxes.Length; i++)
{
//i dont know how to jump to the next cell of current row in the dtg
}
Are there any other options besides textbox arrays?除了文本框 arrays 之外,还有其他选项吗?
I hope I understand the question right and you're looking for manual binding of Textbox
es to DataGridView
.我希望我能正确理解这个问题,并且您正在寻找将Textbox
es 手动绑定到DataGridView
。 Note, that there's also binding built into the Windows Forms.请注意,Windows Forms 中还内置了绑定。
Prerequsities in the following example:以下示例中的先决条件:
DataGridView
named dg
名为dg
DataGridView
Name
set每列都有Name
集Name
set to string "tb" and the column name (ie "tbID") Texbox 的Name
设置为字符串“tb”和列名(即“tbID”)DataGridView.SelectionMode
is set to FullRowSelect
DataGridView.SelectionMode
设置为FullRowSelect
DataGridView
is not shown in the picture图片中未显示将示例数据集加载到DataGridView
中的按钮C#: C#:
// Sample data set
private void BtnLoadDg_Click(object sender, EventArgs e)
{
DataTable tb = new DataTable();
tb.Columns.Add("ID");
tb.Columns.Add("Model");
tb.Columns.Add("Brand");
tb.Columns.Add("Description");
tb.Rows.Add(
{
1,
"T87",
"Tatra",
"A classic streamlined car."
});
tb.Rows.Add(
{
2,
"L&K A",
"Laurin&Klement",
"First L&K car from 1905."
});
this.dg.DataSource = tb;
}
private void dg_SelectionChanged(object sender, EventArgs e)
{
if (this.dg.SelectedRows.Count > 0)
{
foreach (DataGridViewColumn col in dg.Columns)
{
Control[] tbs = Panel1.Controls.Find("tb" + col.Name, false);
if (!Information.IsNothing(tbs) && tbs.Count > 0)
{
TextBox tb = (TextBox)tbs[0];
DataGridViewCell cell = this.dg.Rows(this.dg.SelectedRows(0).Index).Cells(col.Index);
tb.Text = cell.Value;
}
}
}
else
{
this.tbID.text = "";
this.tbModel.text = "";
this.tbBrand.text = "";
this.tbDescription.text = "";
}
}
VB.NET: VB.NET:
' Sample data set
Private Sub BtnLoadDg_Click(sender As Object, e As EventArgs) Handles BtnLoadDg.Click
Dim tb As New DataTable
tb.Columns.Add("ID")
tb.Columns.Add("Model")
tb.Columns.Add("Brand")
tb.Columns.Add("Description")
tb.Rows.Add({1, "T87", "Tatra", "A classic streamlined car."})
tb.Rows.Add({2, "L&K A", "Laurin&Klement", "First L&K car from 1905."})
Me.dg.DataSource = tb
End Sub
Private Sub dg_SelectionChanged(sender As Object, e As EventArgs) Handles dg.SelectionChanged
If Me.dg.SelectedRows.Count > 0 Then
For Each col As DataGridViewColumn In dg.Columns
Dim tbs() As Control = Panel1.Controls.Find("tb" & col.Name, False)
If Not IsNothing(tbs) AndAlso tbs.Count > 0 Then
Dim tb As TextBox = CType(tbs(0), TextBox)
Dim cell As DataGridViewCell = Me.dg.Rows(Me.dg.SelectedRows(0).Index).Cells(col.Index)
tb.Text = cell.Value
End If
Next
Else
Me.tbID.text = ""
Me.tbModel.text = ""
Me.tbBrand.text = ""
Me.tbDescription.text = ""
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.