Although ASP.NET DataGrid is well known as a very good table control, we cannot praise the editing function of DataGrid. Taking the data submission function of DataGrid as an example, there is indeed a big problem: in DataGrid, every To edit one line, you must submit one line, which is the so-called "single line editing, single line submission". In this case, if too many lines are edited, not only will the user operation be cumbersome, but it will also cause frequent access to the server, greatly reducing system efficiency.
Of course, there is a resurrected solution, which is to transfer the content to be edited to other pages and edit it in TextBox. However, if you think about it carefully, isn't this method deceiving yourself? Also, when we edit in the Grid, we can't always use the Tab key to jump between Grid (TextBox). If the response is Enter Events require programmers to waste a lot of energy on development.
How to solve the above problems? Now I recommend to you a domestic DataGrid that I am using: SmartGrid (can be downloaded from Sky Software Station: http://www.skycn.com/soft/23547.html ). I have been using this control for a long time. Now let’s discuss the multi-row submission method of SmartGrid: SmartGrid does not have the button columns in DataGrid. Instead, the entire form has only one submit button. Whether you change one row or multiple rows, you can submit them all at once, as follows Let’s take a look at some examples:
Example:
The picture above is a better example of editing. The example shows that you can edit multiple lines or one line, and then submit them together.
Code:
Modify the button code:
private void btonSave_Click(object sender, System.EventArgs e)
{
this.DataGrid1.ReadOnly = false;//Enter editing
this.DataGrid1.AllowAdd = true;//Allow adding
this.DataGrid1.AllowDelete = true; //Allow deletion
}
This code is a unique attribute of smartgrid. You can set various functions of adding, deleting, editing,
and saving the button code:
private void Button2_Click(object sender, System.EventArgs e)
{
DataTable t = (DataTable)this.SmartGrid1.DataSource;
this.sqlDataAdapter1.Update(t);
t.Clear();
this.sqlDataAdapter1.Fill(t);
this .SmartGrid1.DataSource = t;
}
This is to submit the data to the database as a whole. This approach is suitable for large amounts of data.
There is also a way to submit the data to the server
code line by line:
private void btonSave_Click(object sender, System. EventArgs e)
{
DataTable tb=(DataTable)this.SmartGrid1.DataSource;
SqlParameter[] parameters=new SqlParameter[5];
foreach(DataRow dr in tb.Rows)
{
parameters[0]=new SqlParameter("@customerId", ""+dr[1]+"");
parameters[1]=new SqlParameter("@companyName",""+dr[0]+"");
parameters[2]=new SqlParameter("@contactName", ""+dr[2]+"");
parameters[3]=new SqlParameter("@contactTitle",""+dr[3]+"");
parameters[4]=new SqlParameter("@address", ""+dr[4]+"");
//EamPd is a class. Execute is a function for executing stored procedures. parameters are the parameters required for stored procedures.
EamPd.Execute("CreatLayer",parameters);
}
}