【问题标题】:Data from one datagridview to another c#.net从一个 datagridview 到另一个 c#.net 的数据
【发布时间】:2014-10-17 19:05:54
【问题描述】:

我在一种形式中有两个数据网格视图,它们有一些相似的列 当用户单击按钮时,第二个数据网格视图的第二和第三列的选定行数据应该转到第一数据网格视图的第一和第二列。 我已使用

检索数据
string strppno = dataGridView2.SelectedCells[1].Value.ToString();
string strpartno = dataGridView2.SelectedCells[2].Value.ToString();

我可以使用

粘贴到第二个 datagridview
dataGridView1.Rows[0].Cells[1].Value = strppno;
dataGridView1.Rows[0].Cells[2].Value = strpartno;

我怎么不知道如何循环它,以便它可以多次使用或多个选定的值。

谁能帮帮我。我提前感谢您的任何建议。

第一次尝试。

for (int index = 0; index < dataGridView2.Rows.Count; index++)
{
int editIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[editIndex].Cells[0].Value = dataGridView2.Rows[index].Cells[1].Value;
dataGridView1.Rows[editIndex].Cells[1].Value = dataGridView2.Rows[index].Cells[2].Value;
}

这会从 datagridview 2 中带出所有内容。我只想要选定的内容。

第二次尝试。

string strppno = dataGridView2.SelectedCells[1].Value.ToString();
string strpartno = dataGridView2.SelectedCells[2].Value.ToString();

for (int i = dataGridView2.SelectedRows.Count; i < dataGridView2.SelectedRows.Count + 1; i++)
{
    dataGridView1.Rows[i].Cells[i].Value = strppno;
    dataGridView1.Rows[i].Cells[i + 1].Value = strpartno;
}
    dataGridView1.Rows.Add(dataGridView2.SelectedRows.Count);
}

这有点带来我想要的东西,但在错误的单元格中加上循环继续运行。 :( 我一直在摆弄第二个循环,但无法获得预期的结果.. :(

【问题讨论】:

  • 在我看来就像一个嵌套的 for 循环。看看迭代二维数组 - 应该对你有帮助:)
  • 我试过了,但我似乎没有给出所需的结果。请给我一个例子。
  • 能否请您展示一下您的尝试,我们看看能否为您指明正确的方向
  • 更新了我的回答。请检查

标签: c# winforms datagridview


【解决方案1】:

这样的东西会对你有用:

private void dataGridView_SelectionChanged(object sender, EventArgs e) {
            foreach (DataGridViewRow row in dataGridView.SelectedRows) {
                string value1 = row.Cells[0].Value.ToString();
                string value2 = row.Cells[1].Value.ToString();
                //...
                //also set the second datagrid from here too
            }
        }

因此,在您的示例中,您只想使用第二个和第三个单元格值,并将它们分配给您的其他数据网格:)


作为旁注,为了使您的变量更具可读性/等等,您应该习惯使用 camelCase,

你的string strpartno 应该变成string strPartNo

以及正确命名 dataGridViews/etc


从您的评论中,我意识到您的 insertInto 代码需要稍作修改:

目前,您放置它们:

dataGridView1.Rows[0].Cells[1].Value = strppno;
dataGridView1.Rows[0].Cells[2].Value = strpartno;
                   ^
                   |
              this will only place it into index 0 of the datagrid

所以,我的建议是先

  int count = ... //count number of rows in SecondDatagrid

然后做:

dataGridView1.Rows[count++].Cells[1].Value = strppno;
dataGridView1.Rows[count++].Cells[2].Value = strpartno;
                   ^
                   |
              this will only place it into next 
              available row of the datagrid              

这样,您仍将插入前两列,但不会覆盖当前存储的值。

【讨论】:

  • 我实际上在按钮按下事件中需要它。我现在就试试这个...我一直使用 camelCase。编辑让我很沮丧……所以最终约定开始改变。 :P :P :P 我会试试这个,让你知道。 :)
  • 它给了我与我第一次尝试相同的结果。一旦我选择并添加,它就会进入 dataGridView。但是如果我需要添加更多行会发生什么?我如何做到这一点
  • 说,dataGridView2一共有10行……我想给dataGridView1加1,3,4,7。我选择 1 .. 单击添加 .. 这有效。现在,当我选择第 3 行并单击添加时,应在第 1 行下方的 dataGridView1 中添加新行,并且应添加类似的其他行。目前它取代了第 1 行。
  • 不起作用 :( :( 计数不断增加的数据不会添加到正确的行中。:(
  • 不会添加到正确的行中 - 你能解释一下吗?
【解决方案2】:

好的,我明白了... 当您向 dataGridView 添加新行时,就会出现问题。它不在现有数据之后添加,而是在其之前添加。因此发生异常。 解决方案如下,以防其他人需要它。 :)

int count =0; // declare in public class

private void button3_Click(object sender, EventArgs e)
    {
        if (dataGridView2.SelectedRows.Count > 0)
        {
         dataGridView1.Rows.Add(dataGridView2.SelectedRows.Count); // add rows before entering data inside the new dataGridView.
        }
        foreach (DataGridViewRow row in dataGridView2.SelectedRows)
        {

            {
                string value1 = row.Cells[0].Value.ToString();
                string value2 = row.Cells[1].Value.ToString();
                dataGridView1.Rows[count].Cells[0].Value = value1;
                dataGridView1.Rows[count].Cells[1].Value = value2;
            }
        }

        count =count + 1;
        dataGridView2.ClearSelection(); // I used this because i have to work with 4 dataGridViews 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多