简体   繁体   中英

How to get gridview all checked row data into textboxes of another form in C#?

I have a data grid view with checkbox column as the 1st column. What we want is when user checked rows, all the checked rows should goes to textboxes in another Form. I wrote followings to do that. But the problem is although checked more than 1 row, always it sends the last checked row data to the next form. Not all checked rows data

private void btngnvoucher_Click(object sender, EventArgs e)
{
    // foreach(DataGridViewRow row in dataGridView1.Rows)
    for (int x = 0; x < dataGridView1.RowCount;x++ )
    {
        // DataGridViewCheckBoxCell ch1  = (DataGridViewCheckBoxCell)row.Cells[0];
        DataGridViewCheckBoxCell ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[x].Cells[0];

        if (ch1.Value != null)
        {
            for (int a = 0; a < 6; a++)
            {
                for (int col = 1; col < 5; col++)
                {
                    TextBox theText1 = (TextBox)vobj.Controls[col - 1];

                    // theText1.Text = row[x].Cells[col].Value.ToString();
                    theText1.Text = dataGridView1.Rows[x].Cells[col].Value.ToString();

                }

                // a = a + 1;
                break;

            }
        }
    }

    vobj.Show();
}
}

}

Can any one tell me what I can do to solve this?

Instead of this:

theText1.Text = dataGridView1.Rows[x].Cells[col].Value.ToString();

try:

theText1.AppendText(dataGridView1.Rows[x].Cells[col].Value.ToString());

The cause of your problem appears to be that you intend the variable a to do something yet don't do anything with it. It looks like this is meant to reference a row of text boxes which are then filled in by the code that looks over the cell.

As it stands this code:

for (int col = 1; col < 5; col++)
{
    TextBox theText1 = (TextBox)vobj.Controls[col - 1];

    // theText1.Text = row[x].Cells[col].Value.ToString();
    theText1.Text = dataGridView1.Rows[x].Cells[col].Value.ToString();

}

Is filling the same four text boxes for every row.


That said, your code has a lot of other problems that when fixed would probably make things clearer for you.

Firstly - whenever possible, use a foreach loop to go over the row and cell collections of the DataGridView . It just ends up much cleaner and more maintainable. For example, when you loop over the columns you want, you presume that another column is never going to be added.

Next - try to reference columns by name rather than by index. It is far less fragile when maintaining the code.

Your check to see if the check box is checked is incorrect - the way you have it if the user selects the box then removes the check you still count it. You need to check for null and if not null check for true.

With those changes you have something like this:

foreach (DataGridViewRow r in dataGridView1.Rows)
{
    if (r.Cells["CheckBox"].Value != null && (bool)r.Cells["CheckBox"].Value)
    {
        foreach (DataGridViewCell c in r.Cells)
        {
            if (c.ValueType == typeof(string))
            {
                // The code here is still ugly - there is almost certainly
                // a better design for what you are trying to do but that is
                // beyond the scope of the question.
                // Plus it still has your original bug of referencing the 
                // same row of text boxes repeatedly.
                TextBox theText1 = (TextBox)vobj.Controls[c.ColumnIndex];
                theText1 += c.Value.ToString();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM