通过循环C#更新数据表问题 [英] Update Datatable problem by looping C#

查看:82
本文介绍了通过循环C#更新数据表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上。在将数据绑定到datagridview2之前,我在数据表中更新时遇到问题。我的想法是从datagridview1获取输入,然后我将首先绑定到datatable并将其传递给datagridview2。我在validateDatagridview1事件处理程序中插入的代码方法。



以下是插入数据的示例

Hai. I'm having problem updating inside datatable before bind it to datagridview2. My idea is getting the input from datagridview1, then I will bind first to datatable and pass it to datagridview2. The code method I inserted at validateDatagridview1 event handler.

Here is the example of data inserted

Column 1 |  Column 2
1        |    A
2        |    B



起初代码工作正常,但当我回去编辑时,抛出的异常在位置1没有行



例如我想添加新数据


At first the code works fine, but when i going back to edit, the exception thrown there is no row at position 1

For example I wanna add new data

Column 1 |  Column 2
1        |    A
2        |    B
3        |    C



这里是代码




here is the code

private void tbtabpage1_Validating(object sender, CancelEventArgs e)
   {
       validateDatagridview1(Datagridview1);
   }

private void validateMetabolite(DataGridView dgv)
    {
        DataColumnCollection columns = Datagridview1.Columns;

        if (!columns.Contains(Datagridview1.Columns[0].Name))
        {
            Datagridview1.Columns.Add(Datagridview1.Columns[0].Name, typeof(int));
        }

        if (!columns.Contains(Datagridview1.Columns[1].Name))
        {
            Datagridview1.Columns.Add(Datagridview1.Columns[1].Name, typeof(string));
        }

        int count = 1;

        for (int rowIndex = 0; rowIndex < Datagridview1.Rows.Count; rowIndex++)
        {
            if (Datagridview1.Rows.Count == 0)
            {
                if (Datagridview1.Rows[0].Cells[1].Value != null)
                {
                    Datagridview1.Rows.Add(count, dbMetName.Rows[0].Cells[1].Value);
                }
            }

            if (Datagridview1.Rows.Count != 0)
            {
                count += rowIndex;  //Count should be increment
                DataRow r = Datagridview1.Rows[rowIndex];
                if (Convert.ToString(r[Datagridview1.Columns[1].Name]) !=
                    Datagridview1.Rows[rowIndex].Cells[1].Value.ToString())
                {
                    dataTable1.Rows.Add(count, Datagridview1.Rows[rowIndex].Cells[1].Value);
                }
            }
        }

推荐答案

count += rowIndex;



鉴于 rowIndex 在每次迭代时递增,这意味着您的计数将增加第一次为0,第二次为1,第三次为2,等等。

从count = 1开始:1,2,4,7,11,16 ...... br />
你看到了这个问题吗?



这不是你代码中唯一的问题。例如:


Given that rowIndex is incremented at each iteration, that means your count will be incremented by 0 the first time, by 1 the second time, by 2 the third time, etc.
Starting from count = 1 : 1, 2, 4, 7, 11, 16, ...
Do you see the problem?

This is not the only issue in your code. For example:

for (int rowIndex = 0; rowIndex < Datagridview1.Rows.Count; rowIndex++)
{
   if (Datagridview1.Rows.Count == 0) {
      // ...
   }
}



由于你将rowIndex定义为严格小于dgv的行数(并且因为你永远不会从中删除任何行),所以这部分代码将没有任何机会被执行。



你绝对应该在validateMetabolite方法的开头设置一个断点,启动调试模式,逐行执行,检查每个点你得到的值以及它们是否符合你的期望或不。有太多明显的错误,你必须学习通过调试来检测它们。



祝你好运,你最终可以寻求调试帮助的帮助。到目前为止,这不是开发中不那么有趣的部分。事实上,这是您将学习最多的地方。


Since you define rowIndex as strictly less than your dgv's rows count (and since you never delete any row from it either), this part of the code won't ever have any chance to get executed.

You definitely should put a breakpoint at the beginning of your validateMetabolite method, launch the debug mode, and execute line by line, checking at each point what values you get and whether they match your expectations or not. There are too many obvious mistakes, you have to learn to detect them with debugging.

Good luck, you can ask for help on debugging itself, eventually. This is not the less interesting part of development, by far. In fact, this is where you will learn the most.


这篇关于通过循环C#更新数据表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆