Using DataGrid (attitude of mine)

Source: Internet
Author: User
Datagrid|datagrid tks!

A, Introduction
The DataGrid is located under the System.Web.UI.WebControls namespace, and when using this namespace, make sure you add System.Web.DLL this assemble (assembly) to the current project reference.

B, subject
In this article I will share some of my preliminary knowledge of the DataGrid with you and how to binding the CheckBoxList control in the DataGrid colums [and so on, you can easily bind other WebControls].

C, Some importants pionts
Events that must be understood:
Datagrid.itemdatabound: Occurs after data is bind
Several classes:
DataGridItem: Represents a row of data, while Datagrid.items is a collection of DataGridItem
TableCell: A cell that represents a table
DataGridItemEventArgs: Inheriting from EventArgs

D, to achieve
private void Datagrid1_itemdatabound (object sender, DataGridItemEventArgs e)
{


DataGridItem item = E.item; Gets the current line, that is, the DataGridItem

int id = Int. Parse (item. Cells[0]. Text); Get the database keyword for the related query, here I put an ID in the first column


CheckBoxList productslist; Declare a CheckBoxList variable
Productslist = new CheckBoxList (); Instance a CheckBoxList object

DataTable products = productaccessor.getproducts (ID); Querying the database based on the ID taken

for (int i = 0; I < products. Rows.Count; i++)
{
ListItem ListItem = new ListItem (); Declare and instance a ListItem
Listitem.value = products. Rows[i][0]. ToString ();
Listitem.text = products. ROWS[I][1]. ToString ();
if (products. ROWS[I][2]. ToString () = = "1")
Listitem.selected = true;
PRODUCTSLIST.ITEMS.ADD (ListItem); Add ListItem to the CheckBoxList.
}

Productslist.selectedindexchanged +=new EventHandler (productslist_selectedindexchanged);

Productslist.autopostback = true;

Item. CELLS[1]. Controls.Add (productslist); Add CheckBoxList to the second cell in the current row

}

}

E
At this point, we have finished adding CheckBoxList to the main work of the DataGrid, to see if our DataGrid has become more humane OH!!!
Gaga, if you have any questions about this article, you can qq25359952,i will VERY HAPPY to help Everyboy. Everybody progress together.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.