{
//存在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;
}
编辑状态下的所有列宽度设置:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.EditItem)
{
for(int i=0;i<e.Item.Cells.Count;i++)
{
if(e.Item.Controls[i].Controls[0].GetType().ToString()=="System.Web.UI.WebControls.TextBox")
{
TextBox tb=(TextBox)e.Item.Controls[i].Controls[0];
tb.Width=Unit.Parse("50px");
}
}
}
}