现在有个项目开始使用ASP.NET 2.0开发,在开发过程中关于GridView的使用的一些零星的记录
1.TemplateField中的绑定
<
asp:TemplateField
>
< ItemTemplate > <% # Eval("OCompany.Name") %> </ ItemTemplate >
</ asp:TemplateField >
< ItemTemplate > <% # Eval("OCompany.Name") %> </ ItemTemplate >
</ asp:TemplateField >
2.通过CheckBox得到选择行的主健值(GridView支持多主健)
private
string
GetSelectId()
{
//EnsureChildControls();
GridView gvlist1 = (GridView)this.Controls[0].FindControl("gvlist");
int icount = gvlist1.Rows.Count;
string id = string.Empty;
for (int i = 0; i < gvlist1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)gvlist1.Rows[i].FindControl("cb_id");
//得到选中行的信息
if (cb.Checked == true)
{
DataKey dkr = gvlist1.DataKeys[i];
id = dkr.Value.ToString();
}
}
return id;
}
{
//EnsureChildControls();
GridView gvlist1 = (GridView)this.Controls[0].FindControl("gvlist");
int icount = gvlist1.Rows.Count;
string id = string.Empty;
for (int i = 0; i < gvlist1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)gvlist1.Rows[i].FindControl("cb_id");
//得到选中行的信息
if (cb.Checked == true)
{
DataKey dkr = gvlist1.DataKeys[i];
id = dkr.Value.ToString();
}
}
return id;
}
3.动态创建TemplateField
private
void
TemplateColumn(GridView gvlist,
string
column, GridListAttribute att)
{
TemplateField tf = new TemplateField();
tf.HeaderText = att.HeaderText;
tf.HeaderStyle.Width = Unit.Parse(att.Width);
ColumnTemplate temp = new ColumnTemplate();
temp.Column = att.Column;
tf.ItemTemplate = temp;
gvlist.Columns.Add(tf);
}
ColumnTemplate类
{
TemplateField tf = new TemplateField();
tf.HeaderText = att.HeaderText;
tf.HeaderStyle.Width = Unit.Parse(att.Width);
ColumnTemplate temp = new ColumnTemplate();
temp.Column = att.Column;
tf.ItemTemplate = temp;
gvlist.Columns.Add(tf);
}
public
class
ColumnTemplate : ITemplate
{
//第一个模板列
public void InstantiateIn(Control container)
{
LiteralControl l = new LiteralControl();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl lt = (LiteralControl)sender;//LiteralControl发送绑定请求
GridViewRow container = (GridViewRow)lt.NamingContainer;
lt.Text = DataBinder.Eval(container.DataItem, column).ToString();
}
}
{
//第一个模板列
public void InstantiateIn(Control container)
{
LiteralControl l = new LiteralControl();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl lt = (LiteralControl)sender;//LiteralControl发送绑定请求
GridViewRow container = (GridViewRow)lt.NamingContainer;
lt.Text = DataBinder.Eval(container.DataItem, column).ToString();
}
}