Are you very disappointed with the editing function of ASP.NET DataGrid? We will introduce you to this article to build your full-featured DataGrid using the .NET basic type library. In web-based applications, directly edit data and update to the database to get an unexpected effect. ASP.NET provides us with many base classes to help us create our own full-featured DataGrid. This article uses the System.Text class to create our DataDrid, then use the System.Data.sqlClient class and database interaction, before use, you must first import them into the application.
We first create a basic form, put two controls above, one is an update button, and the other is a container that displays data. Use the efficient StringBuilder class to generate the HTML string of the data grid. Let's take a look at the code of the data:
Private dataset getds ()
{
System.data.sqlclient.sqlconnection myconn;
String conn = "server = (localhost) // netsdk; database = northwind; trusted_connection = yes";
MyConn = new system.data.sqlclient.sqlConnection (conn);
DS = new dataset ();
DS.casensensitive = true;
SQLCOMMAND SQLCMD = new system.data.sqlclient.sqlcommand ();
Sqlcmd.connection = myconn;
Sqlcmd.commandtext = "Select Customerid, CompanyName, ContactName, ContactTitle from Customers";
Da = New SqlDataAdapter (SQLCMD);
Sqlcommandbuilder scb = new sqlcommandbuilder (da);
Da.selectCommand = SQLCMD;
Da.TableMappings.Add ("Table", "Customers");
Da.fill (DS);
Return DS;
}
DataSet Returns the columns and rows of data in the database, just like a table. In order to fill the grid, we must first write the required HTML, build different column objects, and traverse the entire data table, use the StringBuilder class to generate the HTML code of the page table.
Private void writegrid ()
{
DataTable DT = getds (). Tables [0];
DataRow DR;
StringBuilder SB = New StringBuilder ();
// html form start
Sb.append ("