Typical dataset errors

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.