gpt4 book ai didi

c# - 我需要了解为什么当我向标签添加值时我得到空值?

转载 作者:行者123 更新时间:2023-11-30 17:26:18 25 4
gpt4 key购买 nike

当我检查 CheckBoxList 中的项目时,我正在制作一个表单,我想从 SQL table 中获取哪些列,我得到一个 new Form() 然后我想添加值并将它们添加到 SQL。除了最后一个,我得到空值。任何建议为什么会这样? enter image description here

 private void Button1_Click(object sender, EventArgs e)
{
query = $"INSERT INTO {DM.comboBox1.SelectedItem} Values(";


for (int i = 0; i <DM.checkedListBox1.CheckedIndices.Count; i++)
{
if (DM.checkedListBox1.CheckedIndices.Count == i + 1)
{
query += "'" + txtBox[i].Text + "')";
break;
}
query += "'" + txtBox[i].Text + "'";
query += ",";

}
myQuery = query;

Fm1.conn = new SqlConnection($"Server = {Fm1.ServerBox.Text }; Database = { Fm1.DBBox.Text}; Trusted_Connection = True");
Fm1.cmd = new SqlCommand(myQuery, Fm1.conn);
Fm1.conn.Open();
Fm1.cmd.ExecuteNonQuery();
Fm1.conn.Close();

}

private void Test_Load(object sender, EventArgs e)
{
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
int n = 0;

for (int i = 0; i < DM.checkedListBox1.CheckedItems.Count; i++)
{

txtBox = new TextBox[DM.checkedListBox1.CheckedItems.Count];
labels = new Label[DM.checkedListBox1.CheckedItems.Count];

labels[i] = new Label();
for (int j = i; j < DM.dataGridView1.Columns.Count; j++)
{
if (DM.dataGridView1.Columns[j].Visible)
{
labels[i].Text = DM.dataGridView1.Columns[j].HeaderText;
break;
}
}
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(labels[i]);

txtBox[i] = new TextBox();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(txtBox[i]);
}

Controls.Add(tableLayoutPanel);
}

最佳答案

无法评论,因为没有 50。上面的 Amy 是正确的。

删除这些行并重试:

labels = new Label[DM.checkedListBox1.CheckedItems.Count];
txtBox = new TextBox[DM.checkedListBox1.CheckedItems.Count];

因评论而更新。

关于c# - 我需要了解为什么当我向标签添加值时我得到空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56954095/

25 4 0