Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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

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

C#
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);
                }
            }
        }
Posted
Updated 12-Jun-14 8:14am
v2
Comments
PIEBALDconsult 12-Jun-14 11:05am    
Have you tried using .AcceptChanges ?
http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges(v=vs.110).aspx
arave0521 12-Jun-14 11:09am    
yes. i tried. but still the same bug. The problem because of my algorithm. I still work out on it.
ZurdoDev 12-Jun-14 14:03pm    
You need to reply to the comment so that the user is notified.
johannesnestler 12-Jun-14 11:23am    
Hi arave521, First, I don't understand what you do with the columns (did you mean to copy columns to the mentioned datagrid2 instance which never shows up in your code?). Then in your Loop you ask if the RowCount is zero and if so you access the first row -> this can't work, you see? maybe don't use inefficent if(true) and if(!true) on the same condition, therefore we have if-else which performs better and is clear to the reader - I think in this case you got yourself confused. Btw. I think the whole approach of fiddling arround with UI-controls to get data to a datatable should be solved "better"...

1 solution

C#
count += rowIndex;

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:
C#
for (int rowIndex = 0; rowIndex < Datagridview1.Rows.Count; rowIndex++)
{
   if (Datagridview1.Rows.Count == 0) {
      // ...
   }
}

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.
 
Share this answer
 
v2

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