You must Sign In to post a response.
  • Category: .NET

    How do set datagridviewcheckbox as first column at every insertion of row?

    I am working on datagridview(dgv), and i am stuck in one issue , i.e I want to display data in dgv by taking some value from combobox(cmb). when i press select button it taking value from cmb and get related data from datatable by executing query . Now my issue comes my datatable has 4 columns (a,b,c,d) and i want to display all in dgv. i dont want to bind table to dgv but i want to add first column as dgvcheckbox and then from 2nd column my datatable columns as (2nd -a, 3rd-b, 4th -c,5th -d) like this at every insertion of rows . when query retrieves rows from table rows count may be anything. so if retrieves 2 rows 2 dgvcheckbox should add at 2 rows i.e 1st column of every row.

    Is this possible ? if yes please give me a way thanks !

    here is my code for help:

    private void button3_Click_1(object sender, EventArgs e) //select button
    DateTime date = Convert.ToDateTime(dateTimePicker1.Text);
    string stname = comboBox1.SelectedValue.ToString();
    string datetime = date.ToString("yyyy-MM-dd 00:00:00.000");

    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Proc_GetIndentTable";
    cmd.Connection = con;
    cmd.Parameters.Add(new SqlParameter("@storename", stname));
    cmd.Parameters.Add(new SqlParameter("@date", datetime));
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    dataGridView3.DataSource = dt;
  • #763195
    Hai Priyanka,
    You can handle this using RowCreated or RowDatabound event where you can add the checkboxes dynamically in each rows, first column based on the rows.
    You can get the current row index and if the index is 0, you need to add one checkbox, if the index is 1, then you need to add 2 checkboxes and so on..
    Your existing code is fine, you just need to have another event handler for the checkbox handling.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #763199
    Hello Priyanka,

    You can refer the below code :

    DataGridViewCheckBoxColumn chkbox = new DataGridViewCheckBoxColumn();

    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand("SELECT Memo_No, Prod_Qty, Prod_Rate, Prod_Amount, Auto_ID FROM MemoDetail WHERE Memo_Mode = 'Cash' AND Memo_Date = '" + MemoDate.Text + "'", con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    this.StockView.Rows.Add(chkbox.DataGridView, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString());
    catch (Exception ex)
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    See the attached image. It's an output i have got using above mentioned code.

    Hope this is what you are looking for.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #763210
    Hi Priyanka,

    I have a question why your checkbox is dynamic one, what's the wrong if you are using static checkbox. If checkbox text value is dynamically getting from database then in datatable bind that result first and bind it for all rows, if that is static I request you to bind it in your designer itself.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • Sign In to post your comments