How to use dynamic graphical representations in DataGrid

zhaozj2021-02-16  62

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 = "


";}} It looks very simple? Oh, I hope you will not think that I am lie to you, I have created a page for a page according to the above steps, the effect is good, I want to achieve many other information about data, such as: proportion. However, this ratio calculates that you are best to deal with the DataTable before the bonding, so the code will be very simple. The results of operation are as follows:

All right! This article has been finished, I wish you all a smooth job!

: P

转载请注明原文地址:https://www.9cbs.com/read-23907.html

New Post(0)