-1

I have a problem.Let's explain this: I create a DataTable 'DataT' variable and added row to this variable.SameTime i have datagridview. After i assingmented 'DataT' to DataSource property of DataGridView. I get 'DataT' Rows.Count property.It's value 10.But this value decrease from 10 to 5 when i removed 5 Rows of DataGridView. So why decrease my value i just remove Rows of DataGridView but i did not remove rows of 'DataT' although rows count of 'DataT' decrease from 10 to 5. I have not solve this problem.

EDIT: Yes i bound datatable.But same problem there is other state.I explain with a example:

DataTable Table1 = new DataTable();
DataTable Table2 = new DataTable();
DataRow Rows;

private void Form1_Load(.......)
{
    Tablo1.Columns.Add("Column1");
    Tablo1.Columns.Add("Column2");

    Rows = Table1.NewRow();
    Rows[0] = "Hello";
    Rows[1] = "Word";
    Table1.Rows.Add(Rows);

    Rows = Table1.NewRow();
    Rows[0] = "Hello";
    Rows[1] = "Word";

    Table1.Rows.Add(Rows);

    Table2 = Table1;
    datagridview1.DataSource = Table1;      
    //This datagridview1 has 2 Rows and Table1 has 2 Rows.  

    datagridview1.Rows.RemoveAt(0); 
    //I am Removing one Row of datagridview1.Not from Table1.

    //But Automatic removing Rows from Table1.
    //Result=datagridview1 has 1 Row and Table1 has 1 Row.Why do remove rows from Table1?
   //Even Rows Remove from Table2 when i remove rows from datagridview1.    
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
MusaZ
  • 11
  • 6
  • It's really unclear what you are doing, what the problem is or what you have tried doing to solve it. Especially since there is no code to illustrate any of it – UnholySheep Jan 14 '17 at 16:50
  • 1
    How do you remove the rows from DataGridView? If it is bound to DataTable this binding works in both directions. – H.G. Sandhagen Jan 14 '17 at 17:00
  • ???? What have DataGridView / DataTable in common with byte[]. – H.G. Sandhagen Jan 14 '17 at 18:05
  • Sorry i am so new on this form....I incorrect edited...... I added new command. I thank you. – MusaZ Jan 14 '17 at 18:21
  • The actual issue here is data-binding. The GridView is _supposed_ to pass edit/add/delete on to the DataTable. – H H Jan 14 '17 at 19:45
  • When you see DataBinding as 'a problem', you could try `datagridview1.DataSource = Table1.Rows.ToList();`. I didn't test that. – H H Jan 15 '17 at 09:42
  • I thank you Henk for so useful help.By the way i am sorry to late comment... – MusaZ Feb 03 '17 at 16:05

1 Answers1

1

As Henk Holterman pointed out in comment there are two reasons making Table2 to change

  • since Table1 is used as DataSource for grid any changes in the grid will be reflected in it. This is expected behavior of GridView.

    GridView has its own Rows collection that is updated when one sets DataSource. Any changes to Rows collection (like removing item) are applied to DataTable set as data source. As result datagridview1.Rows.RemoveAt(0); actually indirectly removes row from the object referenced by Table1.

  • Both of your variables (Table1 and Table2) point to the same object - so whenever anything happens to the DataTable (i.e. row removed by GridView) the change is visible when you check either of variables.

    Following code performs "shallow copy" when you seem to expect complete clone similar to behavior of int:

    Table2 = Table1; 
    

    After that assignment both Table1 and Table2 refer to DataTable created on DataTable Table1 = new DataTable(); line.

    See more What is the difference between a deep copy and a shallow copy?

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I thank You for Help and Comment.I am reasearching this Copy Methods."Shallow and Deep Copy". – MusaZ Jan 14 '17 at 19:38
  • 1
    Deep/Shallow copy is not really the core issue here. It's about databinding. The GridView does create its own Rows collection (deep copy). – H H Jan 14 '17 at 19:47
  • Would you explain a little more?I binding just do assigment. So how do i binding? – MusaZ Jan 14 '17 at 20:44
  • @HenkHolterman good point that there are actually 2 somewhat unrelated concerns - I've updated post with your comments (linked to comment on question too) - please check if you have time (and mark this comment obsolete when done). – Alexei Levenkov Jan 15 '17 at 02:10
  • So have to i use Deep Copy?.Or Second variable is independent from variable1.Have to Both variable add Rows for each separataly?. – MusaZ Jan 15 '17 at 07:47