DataTable檢測是否存在某個DataColumn
1.if (dt.Columns.Contains("aa") == true)
{
//存在aa
}
2.foreach(DataColumn mDCol in dt.Columns)
{
if(mDCol.ColumnName=="aa")
{
//存在aa
}
}
怎麼樣移除DataGrid列
private void OnDataBound(object sender, Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells.Remove(e.Item.Cells[0]);
}
有條件地格式化DataGrid單元格
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;
// 獲取第四列的值
Int32 nUnitsInStock = Convert.ToInt32(rv.Row.ItemArray[4]);
if (nUnitsInStock < 20)
{
e.Item.Cells[4].BackColor = Color.Red;
}
}
}
DataGrid列寬度設置
浏覽狀態下的列寬度可以這樣設置:
Column.ItemStyle.Width = 100;
編輯狀態下的列寬度可以這樣設置:
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;
}