gpt4 book ai didi

c# - 在 Datagridview 复选框中显示选中的列值,以另一种形式显示数据 Datagridview

转载 作者:行者123 更新时间:2023-11-30 18:20:52 28 4
gpt4 key购买 nike

我有一个 datagridview(dataGridView1,其中包含一个 dataGridview2 列标题),其中我有一个复选框列,称为 dgvCkb,在 dataGridView1。当我在任何行中取消选中 dgvCkb 时,该行的数据不应显示在另一个 dataGridview2 中(dataGridview2 已连接到数据库(本地主机) ).

第一种形式的 dataGridview 是:

enter image description here

第二种形式的dataGridview是:

enter image description here

我的编码:

form1编码

private void Customized_column_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 2;
DataGridViewCheckBoxColumn dgvCkb = new DataGridViewCheckBoxColumn();
dgvCkb.ValueType = typeof(bool);
dgvCkb.Name = "Chk";
dgvCkb.HeaderText = "vis.";
dataGridView1.Columns.Add(dgvCkb);

dataGridView1.Columns[0].Name = "Column";
dataGridView1.Columns[1].Name = "Column size";

string[] row = new string[] {"Date", "103" };
dataGridView1.Rows.Add(row);
row = new string[] {"Time", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Callee Number", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "User", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Called Number", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Department", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Trunk", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "City", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Duration", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Pulse", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Amount", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Status", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Callee Extension", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Trunk Port", "103" };
dataGridView1.Rows.Add(row);
row = new string[] { "Call Type", "103" };
dataGridView1.Rows.Add(row);

}

form2编码

String MyConString = "SERVER=localhost;" +
"DATABASE=test;" +
"UID=root;" +
"PASSWORD=asterisk";

private void PBX_Logger_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(MyConString);
MySqlCommand cmd = new MySqlCommand("SELECT date as Date,time as Time,user as User,department as Department,trunk as Trunk,city as City,calleenumber as CalleeNumber,callednumber as CalledNumber,duration as Duration,pulse as Pulse,amount as Amount,status as Status,calleextension as CalleeExtension,trunkport as TrunkPort,calltype as CallType from pbx", con);
con.Open();
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dataTable);
dataGridView1.DataSource = dataTable;
}

我是开发新手,所以正在寻找解决方案。请给我一些链接。

谢谢

问候禅那

最佳答案

为简单起见,我们假设基于您的 Load 方法句柄的表单名称相应匹配:

Form1.Load += Customized_column_Load;
Form2.Load += PBX_Logger_Load;

我还假定您正在从 Form1 中打开第二个表单,如下所示:

Form2 form2 = new Form2();
form2.ShowDialog();

因此,第二个表单只需要访问第一个表单的网格值。如果将 DataGridView 传递给 Form2 构造函数,则 Form2 可以将它保存到一个变量中并在加载时调用它 - 其中行可以循环访问,并在第二个表单的 DataGridView 上访问和使用大小和可见性值:

private DataGridView parentDGV;

public Form2(DataGridView parentDGV)
{
InitializeComponent();
this.parentDGV = parentDGV;
}

private void PBX_Logger_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;

// Your code to fill the DataSource.

foreach (DataGridViewRow row in parentDGV.Rows)
{
string colName = row.Cells["Column"].Value.ToString();
int size = 0;
int.TryParse(row.Cells["Column size"].Value.ToString(), out size);

dataGridView1.Columns[colName].Width = size;
dataGridView1.Columns[colName].Visible = row.Cells["Chk"].Value != null && (bool)row.Cells["Chk"].Value;
}
}

最后,在 Form1 中,相应地更改您的调用:

Form2 form2 = new Form2(dataGridView1);
form2.ShowDialog();

这还假设以下内容也在 Form1 中设置:

dataGridView1.AllowUserToAddRows = false;

否则,在遍历 Form2 中的行时,您需要添加一些空值检查。

关于c# - 在 Datagridview 复选框中显示选中的列值,以另一种形式显示数据 Datagridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36711233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com