其实很简单,就是在绑定数据后对Cells属性进行操作。
设置Cells.Visible,RowSpan,ColSpan 属性等。
private void SetTableStyle()
{
DataTable dt = new DataTable();
dt.Columns.Add("id",typeof(string));
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("title",typeof(string));
for(int i = 0; i< 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = "name " + i.ToString();
dr[2] = "title " + i.ToString();
dt.Rows.Add(dr);
}
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
int rowspan = 3;
if ( this.DataGrid1.Items.Count > 0 )
{
for( int i = 0 ; i < this.DataGrid1.Items.Count; i ++ )
{
if ( i < 3 )
{
if ( i == 0 )
this.DataGrid1.Items[i].Cells[0].RowSpan = rowspan;
else
{
this.DataGrid1.Items[i].Cells[0].Visible = false;
}
}
else
{
this.DataGrid1.Items[i].Cells[0].Visible = false;
this.DataGrid1.Items[i].Cells[2].ColumnSpan = 2;
}
}
}
设置Cells.Visible,RowSpan,ColSpan 属性等。
private void SetTableStyle()
{
DataTable dt = new DataTable();
dt.Columns.Add("id",typeof(string));
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("title",typeof(string));
for(int i = 0; i< 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = "name " + i.ToString();
dr[2] = "title " + i.ToString();
dt.Rows.Add(dr);
}
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
int rowspan = 3;
if ( this.DataGrid1.Items.Count > 0 )
{
for( int i = 0 ; i < this.DataGrid1.Items.Count; i ++ )
{
if ( i < 3 )
{
if ( i == 0 )
this.DataGrid1.Items[i].Cells[0].RowSpan = rowspan;
else
{
this.DataGrid1.Items[i].Cells[0].Visible = false;
}
}
else
{
this.DataGrid1.Items[i].Cells[0].Visible = false;
this.DataGrid1.Items[i].Cells[2].ColumnSpan = 2;
}
}
}

浙公网安备 33010602011771号