为了账号安全,请及时绑定邮箱和手机立即绑定

我需要用 datagridview 替换 tablelayout

我需要用 datagridview 替换 tablelayout

C#
慕沐林林 2023-06-25 13:37:56
如何制作自动调整大小的 datagridview 而不将其添加到表单上。现在我的代码是tableLayout,但我需要将其替换为DataGridView for (int i = 0; i < dataManipulator.columnCheckedList.CheckedItems.Count; i++)        {   labels[i] = new Label();            labels[i].Text = visibleColumns[i];            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(2, m++));            tableLayoutPanel.Controls.Add(txtBox[i]);            getSqlColumnTypes += $"'{labels[i].Text}'";            SqlDataAdapter sqladapter = new SqlDataAdapter($"{getSqlColumnTypes}", loginForm.connection = new SqlConnection($"Server = {loginForm.serverName.Text }; Database = { loginForm.DBNames.SelectedItem}; Trusted_Connection = True"));            DataSet dt = new DataSet();            sqladapter.Fill(dt);            labels[i] = new Label();            labels[i].Text = dt.Tables[0].Rows[0][0].ToString();            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));            tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(1, k++));            tableLayoutPanel.Controls.Add(labels[i]);       }
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

解决方案在这里:


for (int i = 0; i < dataManipulator.columnCheckedList.CheckedItems.Count; i++)

  {

       addRecordDataGridView.Rows.Add(visibleColumns[i]);

       getSqlColumnTypes += $"'{visibleColumns[i]}'";

       SqlDataAdapter sqladapter = new SqlDataAdapter($"{getSqlColumnTypes}", loginForm.connection = new SqlConnection($"Server = {loginForm.serverName.Text }; Database = { loginForm.DBNames.SelectedItem}; Trusted_Connection = True"));

       DataSet dt = new DataSet();

       sqladapter.Fill(dt);

       addRecordDataGridView.Rows[i].Cells[1].Value = dt.Tables[0].Rows[0][0].ToString();

       addRecordDataGridView.Rows[i].Cells[2].Tag = new DataGridViewTextBoxColumn();

   }


查看完整回答
反对 回复 2023-06-25
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信