设置控件GridView的列宽及动态创建模板列
protected void Page_Load(object sender,EventArgs e)
{
InitialGridVew();
GridView1.OnRowDataBound += GridView1_RowDataBound;
DataTable table = CreateDataTable();
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void InitialGridView()
{
BoundField boundField = new BoundField();
boundField.HeaderText = "Bound Field";
boundField.DataField = "BoundField";
this.GridView_table.Columns.Add(boundField);
ButtonField buttonField = new ButtonField();
buttonField.ButtonType = ButtonType.Button;
buttonField.HeaderText = "Button Field";
buttonField.DataTextField = "ButtonField";
this.GridView_table.Columns.Add(buttonField);
CheckBoxField checkBoxField = new CheckBoxField();
checkBoxField.HeaderText = "CheckBox Field";
checkBoxField.DataField = "CheckBoxField";
this.GridView_table.Columns.Add(checkBoxField);
ImageField imageField = new ImageField();
imageField.HeaderText = "Image Field";
imageField.DataImageUrlField = "ImageField";
this.GridView_table.Columns.Add(imageField);
}
protected DataTable CreateDataTable()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("BoundField", typeof(string)));
table.Columns.Add(new DataColumn("ButtonField", typeof(string)));
table.Columns.Add(new DataColumn("CheckBoxField", typeof(bool)));
table.Columns.Add(new DataColumn("ImageField", typeof(string)));
for (int i = 0; i < 10; i++)
{
DataRow newrow = table.NewRow();
newrow["BoundField"] = "BoundField" + i.ToString();
newrow["ButtonField"] = "ButtonField" + i.ToString();
newrow["CheckBoxField"] = (i % 2 == 0) ? true : false;
string imageFile = @"resource\home.gif";
newrow["ImageField"] = imageFile;
table.Rows.Add(newrow);
}
return table;
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Width = Unit.Pixel(100);
}
}
{
InitialGridVew();
GridView1.OnRowDataBound += GridView1_RowDataBound;
DataTable table = CreateDataTable();
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void InitialGridView()
{
BoundField boundField = new BoundField();
boundField.HeaderText = "Bound Field";
boundField.DataField = "BoundField";
this.GridView_table.Columns.Add(boundField);
ButtonField buttonField = new ButtonField();
buttonField.ButtonType = ButtonType.Button;
buttonField.HeaderText = "Button Field";
buttonField.DataTextField = "ButtonField";
this.GridView_table.Columns.Add(buttonField);
CheckBoxField checkBoxField = new CheckBoxField();
checkBoxField.HeaderText = "CheckBox Field";
checkBoxField.DataField = "CheckBoxField";
this.GridView_table.Columns.Add(checkBoxField);
ImageField imageField = new ImageField();
imageField.HeaderText = "Image Field";
imageField.DataImageUrlField = "ImageField";
this.GridView_table.Columns.Add(imageField);
}
protected DataTable CreateDataTable()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("BoundField", typeof(string)));
table.Columns.Add(new DataColumn("ButtonField", typeof(string)));
table.Columns.Add(new DataColumn("CheckBoxField", typeof(bool)));
table.Columns.Add(new DataColumn("ImageField", typeof(string)));
for (int i = 0; i < 10; i++)
{
DataRow newrow = table.NewRow();
newrow["BoundField"] = "BoundField" + i.ToString();
newrow["ButtonField"] = "ButtonField" + i.ToString();
newrow["CheckBoxField"] = (i % 2 == 0) ? true : false;
string imageFile = @"resource\home.gif";
newrow["ImageField"] = imageFile;
table.Rows.Add(newrow);
}
return table;
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Width = Unit.Pixel(100);
}
}