ASP. NET -- Data bound to the GridView control, asp. netgridview
ASP. NET provides many data server controls for displaying table data in a database on a Web page. The GridView control is one of them. This control is almost the same as the DataGridView control we have learned before, so we are no stranger to the GridView control. The following describes how to use it.
Front-end:
Find the GridView control in the toolbox and drag it to the code editing area.
Step 1: Go to the design page. There is a black triangle to the right above the GridView control. Click this button and select Edit column,
Step 2: remove the check box before the automatically generated field and select BoundField Based on the bound content.
Step 3: Set the title name HeaderText and the bound field name DaTaField.
Step 4: If you want to set a uniform style for each row of the form, select to convert each field to TemplateField.
After these steps are configured, HTML code is automatically generated.
<Div id = "newnews" class = "commonfrm">
Background code:
Protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {// when the page is accessed for the first time, NewsManager nm = new NewsManager (); // bind the latest news gvNewNews. dataSource = nm. selectNewNews (); gvNewNews. dataBind (); // bind to hot news gvHotNews. dataSource = nm. selectHotNews (); gvHotNews. dataBind ();}}
Comparison:
In the niugu news publishing system, we also talked about a data binding control called the Repeater control. The Repeater control does not have its own built-in rendering function, this means that you must create a template to provide the layout of the Repeater control. You need to use ItemTemplate to define the template.
Compared with the Repeater control, the GridView control is more powerful and flexible, and can customize the style of each column in each row. However, a bad drawback is that there are too many redundant code to be generated.
In aspnet, the data source bound to the gridview control is an empty table and data needs to be added to the gridview through the buttn control. How can I write the code?
Declare a global dataset as a static variable,
The code in the button is as follows:
DataTable dt = (DataTable) ds. Tables [0];
DataRow dr = dt. NewRow ();
Dr ["title"] = "123 ";
Dr ["newsinfo"] = "123 ";
Dr ["newtime"] = System. DateTime. Now;
Dt. Rows. Add (dr );
This. GridView1.DataSource = dt;
This. GridView1.DataBind ();
In this way, a new row of data is inserted, but it is not added to the database. You add it to the database by yourself,
I think this is not suitable for you. If one of your databases is a self-increasing primary key, you cannot insert the value. If you do this again, it will lead to errors! I personally think
In Aspnet, how do I write code (C #) for the Gridview control to bind data in the background #)?
String SQL = "server =.; database = your database; integrated security = sspi ;";
String s = "select SoftwareID, SoftwareName from Software ";
SqlConnection conn = new SqlConnection (SQL );
SqlDataAdapter sda = new SqlDataAdapter (s, conn );
DataSet ds = new DataSet ();
Sda. Fill (ds );
GridView1.DataSource = ds;
GridView1.DataBind ();