This article will be simple to introduce how to use a graphic table in DataGrid, it sounds like to use the GUI programming, in fact, if you read a full text, you will find that it is very simple, just a "small trick", But please don't be disappointed, in fact, this "small trick" in the actual application is a highlight in your project.
First, in order to achieve this feature, we need a DataGrid, and for this DataGrid bonding data, the specific bonding code is as follows (because I have explained the problem of the problem, I am using the most primitive bonding method, the purpose In the description topic).
Draw a DataGrid inside HTML, the code is like the following, I used a test database and a test table name called Test, and this TEST table has a, b, c3 field:
As you can see, we used a template column to display the data, it's just a label instead of image, the other two is the bond field, c is the number we have to display.
OK, let's take a look at the part of the CS, help the code as follows:
SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]); SqlDataAdapter da = new SqlDataAdapter ( "select a, c from test", conn); DataSet ds = new DataSet (); da.Fill ( DS); this.DataGrid1.datasource = ds.tables [0]; this.DataGrid1.databind ();
very simple. Because I don't like to write the data bonding code in HTML, I use the itemDatabase event to complete this matter. The specific code is as follows:
private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {Label lbl = (Label E.Item.cells [1] .FindControl ("label1"); lbl.text = "
All right! This article has been finished, I wish you all a smooth job!
: P