Hot questions about the DataGrid Web control

Source: Internet
Author: User
Tags empty first row
Datagrid|web| Control | issues
Add a record to a data source using the DataGrid control
The DataGrid control allows users to view and edit records, but does not include the convenience of adding new records internally, but there are different ways to add this functionality. The following methods include the following steps:
L Add a new, empty record to the DataGrid data Source (dataset or DataBase) and, if necessary, specify an ID for the record and a placeholder value for each column that is not allowed to be empty.
L REBIND the DataGrid to the data source.
L TURN the DataGrid into edit mode, and you need to be able to know where the new record is displayed in the DataGrid.
L Update the record in a normal way--the user clicks Update and then writes the user-supplied value to the data source as the value of the new record.
Here's an example of adding a new record, binding a DataGrid, and translating it into edit mode. The data in this example
The source is a dataset (DsBook1) that contains a books table.
private void Btnaddrow_click (object sender, System.EventArgs e)
{
DataRow dr = This.dsBooks1.Books.NewRow ();
dr["title"] = "(New)";
dr["Instock"] = true;
This.dsBooks1.Books.Rows.InsertAt (DR, 0);
session["dsbooks"] = DSBOOKS1;
Datagrid1.edititemindex = 0;
Datagrid1.databind ();
}
Some of the issues to be noted:
L The code is executed when the user clicks on an "ADD" button in the face.
L new rows are generated by the NewRow () method, the table in the dataset is then inserted through the InsertAt () method, which allows the row to be set to a specific predetermined position--in this case, the first record in the table (that is, the first row in the Rows collection). In other ways, you can also insert the number of rows into the footer by setting the value of the row. It is important to know exactly where the row is in the table.
L Because now you already know that the line is in the first position in the table, you can set the new row to edit mode by setting the edit item index of the DataGrid to 0 (if you create a new row elsewhere in the table, you can set the edit item index to the value in that location).
L now have a new record in the dataset (but it's not in the database), and if you don't want to repopulate the DataGrid from the database and lose new records, you need to keep a copy of the dataset in the process of submitting-return. The example is saved in the session by code, and when the page loads, the dataset needs to be reloaded from the session. The following example is a possible form of the Page_Load () event handle:
private void Page_Load (object sender, System.EventArgs e)
{
if (this. IsPostBack)
{
DSBOOKS1 = (dsbooks) session["Dsbooks"];
}
Else
{
This.sqlDataAdapter1.Fill (THIS.DSBOOKS1);
session["dsbooks"] = DSBOOKS1;
This. Datagrid1.databind ();
}
}
(unfinished)

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.