简体   繁体   English

如何在C#中使gridview将所有检查的行数据放入另一种形式的文本框中?

[英]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. 但是问题是尽管检查了多于1行,但总是将最后检查的行数据发送到下一个表单。 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. 问题的原因似乎是您打算让变量a进行某些操作,但不对其执行任何操作。 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 . 首先-尽可能使用foreach循环遍历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. 您需要检查是否为null,如果不为null,则检查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();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将gridview的选择行数据获取到下拉列表和文本框中 - How to Get select row data of gridview into dropdown list and textboxes 如何在gridview行中查找所有文本框? - How to find all textboxes in a gridview row? 获取datagridview行数据到另一个窗体上的文本框 - getting datagridview row data to textboxes on another form 如何在 WPF C# 中为 DataGrid 获取 TextBoxes 中选定的行值 - How to get the selected Row values in TextBoxes for DataGrid in WPF C# 如何通过使用c#中的if条件在另一个文本框上输入event来从数据库获取数据到文本框 - how to get data to textboxes from database by enter event of on another textbox with if condition in c# C#在表单中添加所有文本框 - C# Add all TextBoxes in a Form 如何使用 mvvm 将 gridview 选定的行数据发送到另一个 window 与 wpf 中的文本框使用 mvvm - How to send gridview selected row data to another window with textboxes in wpf using mvvm 在C#中选中1复选框时,如何通过单击清空所有文本框 - How can empty all textboxes by click when 1 check box is checked in C# 如何在WPF Gridview中获取检查的行值 - How to get checked row values in WPF Gridview C#如何从数据库获取数据到文本框 - C# How do I get data from Database to textboxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM