After years of development, I only know how to use the basic functions of dataset! Just using dataset as the container for offline data is an insult to regard dataset as a powerful space. Therefore, we decided to use a week to thoroughly study dataset.
First, let's list the problems that occurred during use!
Step 1: bind dataset to daggateve. The Code is as follows:
Private void setui (Dataset DS)
{
Try
{
Bindingsource mybs = new bindingsource ();
Mybs. datasource = Ds. Tables [0]. defaultview;
Datagridview1.datasource = mybs;
}
Catch (exception ERR)
{MessageBox. Show (ERR. Message );}
Finally
{}
}
Step 2: do not modify the data in cells in the datagrideview. Directly execute the following code.
The result is that the related columns in dgv show that the "test problem" result is correct.
Private void form1_keydown (Object sender, keyeventargs E)
{
If (E. keycode = keys. F4)
{
If (maid = NULL)
{
Return;
}
Datagridviewrow = datagridview1.currentrow;
Datarow = (maid. databounditem as datarowview). row;
Datarow [1] = "test problem ";
Datarow. acceptchanges ();
// Label6.text = maid. Tag. tostring ();
Label6.text = datarow. rowstate. tostring ();
}
Step 2: Modify the data in cells in the datagrideview. Directly execute the following code.
The result is that the related columns in dgv show that the "test problem" result is correct.
Private void form1_keydown (Object sender, keyeventargs E)
{
If (maid = NULL)
{
Return;
}
Step 4: Modify the data in cells in the datagrideview. Directly execute the following code.
The result is that the related column in dgv shows the "test problem", but the result is that the cell modified in dgv is still the original value, and the result is incorrect.
Private void form1_keydown (Object sender, keyeventargs E)
{
If (E. keycode = keys. F4)
{
If (maid = NULL)
{
Return;
}
Datagridviewrow = datagridview1.currentrow;
Datarow = (maid. databounditem as datarowview). row;
Datarow [1] = "test problem ";
Datarow. acceptchanges ();
// Label6.text = maid. Tag. tostring ();
Label6.text = datarow. rowstate. tostring ();
}
The above test code knows that data in dataset can be updated simply by modifying the code in dgv or directly copying the code to dataset. Then I tested the following code,
Step 5 modify the data in cells in the datagrideview. Directly execute the following code.
The result is that the related column in dgv shows the "test problem", but the result is that the cell modified in dgv is still the original value, and the result is incorrect.
Private void form1_keydown (Object sender, keyeventargs E)
{
If (E. keycode = keys. F4)
{
If (maid = NULL)
{
Return;
}
Datagridviewrow = datagridview1.currentrow;
Datarow = (maid. databounditem as datarowview). row;
Datarow. acceptchanges ();
Datarow [1] = "test problem ";
Datarow. acceptchanges ();
// Label6.text = maid. Tag. tostring ();
Label6.text = datarow. rowstate. tostring ();
}
Through the test of the above five codes, we can draw the following conclusion: to modify the value in dataset, you can either bind the control, modify the data in the space, and then submit the acceptchanged () method to dataset, either directly modify the dataset and submit accceptchanged () to update the data in the dataset. If the two are mixed, only the dataset value can be assigned directly,
How can this problem be solved?