Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview(dgv) , in which i adding first column as dgvcheckboxcolumn and i am able to extract value of second column whose checkboxcell checked. but i am getting a different type of error that is "when i check 1st row checkbox its value not retrived but when i check any other row check box previously checked row value retrived" , but i want to extract instant value , i.e if i checked 1st rows's checkbox i want 1st rows 2nd column value.

here is my code i know something simple mistake i did but cant trace where is error ,

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
   try
   {
     List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();

     foreach (DataGridViewRow row in dataGridView1.Rows)
     {
        if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
        {
         rows_with_checked_column.Add(row);
         data = row.Cells[1].Value.ToString();
        }
       row.Cells[SerialNo.Name].Value = false;
     }

    ifile.IniWriteValue("Config Section", "INDENT_NO", data);
    txtindentcode.Text = data;
    txtfromstore.Text = fm;
     txttostore.Text = to;
} 
Posted
Comments
[no name] 8-Dec-15 3:54am    
It is problem only with 1st row checkbox only? For other row checkboxes you are getting value?
Member 11543226 8-Dec-15 5:05am    
i found solution to this problem by following line,
data = dataGridView1.Rows[index].Cells[1].Value.ToString();
But,
"if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)" this condition false for thirst check after that it gives proper values , whts wrong with this code?
Member 11543226 8-Dec-15 4:04am    
for all rows checkboxex when i check any box at that time in cellClick event it gets null value (i.e no box checked), and then i checkd another box in cellclick event it gets previously checked box rows's value.

1 solution

Use like below code:

C#
       public Form1()
        {
            InitializeComponent();

            dgvEmployee.DataSource = new Employee().GetSampleData();
            DataGridViewCheckBoxColumn colChk = new DataGridViewCheckBoxColumn();
            colChk.DisplayIndex = 0;
            dgvEmployee.Columns.Add(colChk);
            dgvEmployee.RowHeadersVisible = false;
        }

        private void btnGetChecked_Click(object sender, EventArgs e)
        {
            List<employee> lstEmp = new List<employee>();
            foreach (DataGridViewRow row in dgvEmployee.Rows)
            {
                DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(chk.Value) == true)
                {
                    lstEmp.Add(new Employee { FirstName = row.Cells[1].Value.ToString(), LastName = row.Cells[2].Value.ToString(), Salary =Convert.ToInt32(row.Cells[3].Value.ToString()) });
                }

            }

           
        }

</employee></employee>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900