Several tips for a DataGrid!

Source: Internet
Author: User
Tags bind
datagrid| techniques One: Use template columns

We add a template column to the DataGrid that binds the data, placing a button in the column
<asp:TemplateColumn>
<ItemTemplate>
<asp:button id= "Btndelete" runat= "server" text= "Delete" ></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
Then we add the following code in the ItemDataBound of the DataGrid
Switch (e.item.itemtype)
{
Case (ListItemType.Item):
Case (ListItemType.AlternatingItem):
{
Button btn= (button) E.item.findcontrol ("Btndelete");
Btn. Attributes.Add ("OnClick", "Return Window.confirm (' FS ')");
Break
}
}
The DataGrid triggers this event when the data is bound, and each row is triggered once. We can get the line through E.item, and through E. Item.itemtype gets the row type.

Method Two: Use a button column

We add a Delete button column to the DataGrid where the data is bound.
<asp:buttoncolumn text= "Delete" commandname= "delete" ></asp:ButtonColumn>
Then we add the following code in the ItemDataBound of the DataGrid
Switch (e.item.itemtype)
{
Case (ListItemType.Item):
Case (ListItemType.AlternatingItem):
{
LinkButton btn= (LinkButton) e.item.cells[4]. Controls[0];
Btn. Attributes.Add ("OnClick", "Return Window.confirm (' FS ')");
Break
}
}
You can see that we have a different way of getting control references here, using E. ITEM.CELLS[4]. Controls[0] instead of using E. Item.findcontrol ("Btndelete"), as I


Now we are using the button column, we cannot set the id attribute on the button column button, and the FindControl is to find the control by ID (of course we can also use the preceding code


E.ITEM.CELLS[4]. CONTROLS[0] code to replace the.



Add Ordinal column

Bind a DataGrid.
Switch (e.item.itemtype)
{
Case (ListItemType.Item):
Case (ListItemType.AlternatingItem):
{
DataGridItem row= (DataGridItem) E.item;
TableCell cell=new TableCell ();
Cell. Controls.Add (New LiteralControl (e.item.itemindex+1). ToString ()));
Row. Cells.addat (0,cell);
Break
}
Case (Listitemtype.header):
{
DataGridItem row= (DataGridItem) E.item;
TableCell cell=new TableCell ();
Cell. Controls.Add (New LiteralControl ("serial number"));
Row. Cells.addat (0,cell);
Break
}
}
Here we use the E. Item.itemindex, gets the index of the DataGridItem object from the Items collection of the DataGrid control.


Produces two rows of header rows

Bind a DataGrid, set allow paging, set page navigation to upper and lower, and we will force the above page navigation to be replaced with header row. Here I want to talk about the row composition of the DataGrid
Top pager, used to place paging navigation, then header header, then item and AlternatingItem items and alternating items (and of course SelectedItem selected and EditItem


Edit items, and so on), then footer the foot of the table, there is a pager at the bottom. Because the pager line is automatically generated by the system, this row is not captured in the ItemDataBound event, where I use the


ItemCreated event, add code as follows
Switch (e.item.itemtype)
{
Case (Listitemtype.pager):
{
if (a)
{
DataGridItem row= (DataGridItem) E.item;
Row. Cells.clear ();
TableCell cell0=new TableCell ();
Cell0. Controls.Add (New LiteralControl ("ID"));
TableCell cell1=new TableCell ();
Cell1. columnspan=2;
Cell1. Controls.Add (New LiteralControl ("FullName"));
Row. Cells.add (cell0);
Row. Cells.add (CELL1);
}
First=!first;
Break
}
}
The one here is used to judge whether the pager or the pager below.



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.