DataGrid column Operations

Source: Internet
Author: User

DataTable detects if there is a DataColumn
1.if (dt. Columns.contains ("AA") = = True)
{
Presence of AA
}

2.foreach (DataColumn mdcol in dt. Columns)
{
if (mdcol.columnname== "AA")
{
Presence of AA
}
}


How to remove a DataGrid column
private void OnDataBound (object sender, Web.UI.WebControls.DataGridItemEventArgs e)
{
E.item.cells.remove (E.item.cells[0]);
}
Conditionally formatting a DataGrid cell

private void Ondatagriddatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.Item | |
E.item.itemtype = = ListItemType.AlternatingItem)
{
DataRowView RV = (DataRowView) e.Item.DataItem;
Gets the value of the fourth column
Int32 nunitsinstock = Convert.ToInt32 (rv. ROW.ITEMARRAY[4]);
if (Nunitsinstock < 20)
{
E.ITEM.CELLS[4]. BackColor = color.red;
}
}
}
DataGrid Column Width setting
The column widths in the browse state can be set like this:
Column.ItemStyle.Width = 100;

The column widths in edit state can be set like this:
private void Datagrid1_editcommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGridItem line = Dbgcustomers.items[e.item.itemindex];
TextBox tb1 = (textbox) line. Cells[0]. Controls[0];
TextBox TB2 = (textbox) line. CELLS[1]. Controls[0];

Tb1. Width = unit.percentage (100);
TB2. Width = unit.percentage (100);
TB2. TextMode = TextBoxMode.MultiLine;
}

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.