3

I have one datagridview(dataGridView1 which contains a dataGridview2 column header) in which I have one checkboxcolumn, called dgvCkb, in dataGridView1. When I uncheck dgvCkb in any row the data of that row should not be shown in another dataGridview2(dataGridview2 is connected to a database(localhost)).

1st form dataGridview is:

enter image description here

2nd form dataGridview is:

enter image description here

My Coding for this:

form1 coding

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 coding

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;
}

I am new to development so looking for solution. please refer me some link.

Thank You

Regards Dhana

Karthi
  • 100
  • 13
dhana
  • 83
  • 1
  • 9
  • Have you looked at `DataGridView` events: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged(v=vs.110).aspx – Draken Apr 19 '16 at 07:29

1 Answers1

0

For simplicity, let's assume the form names based on your Load method handles match accordingly:

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

I have also assumed that you are opening the second form from the Form1 as follows:

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

As such, the second form just needs access to the first form's grid values. If you pass in that DataGridView into the Form2 constructor, then Form2 can save it into a variable and recall it during load time - where the rows can be looped through and the size and visibility values are accessed and used on the second form's 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;
    }
}

Lastly, in Form1, change your call accordingly:

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

This also assumes the following is set in Form1 too:

dataGridView1.AllowUserToAddRows = false;

Otherwise you'll need to add some null checks when looping through the rows inside Form2.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • Thank you for your answer but I got an Error in this line : 'dataGridView1.Columns[colName].Visible = (bool)row.Cells["Chk"].Value;' as 'An unhandled exception of type 'System.NullReferenceException' occurred in PBX Logger.exe Additional information: Object reference not set to an instance of an object.' now what i do? sorry for trouble. – dhana Apr 21 '16 at 04:30
  • Sorry about that, I didn't account for a row that never had the `CheckBox` column clicked. It would be `null` until that point. See the edit to that line of code in my answer above. – OhBeWise Apr 21 '16 at 14:53