显示选中的列Datagridview中的值复选框,显示另一种形式的数据Datagridview

时间:2016-04-19 07:20:09

标签: c# checkbox datagridview

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

第一个表格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;
}

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

谢谢

此致 Dhana

1 个答案:

答案 0 :(得分:0)

为简单起见,我们假设基于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内的行时,您需要添加一些空值检查。