DataGrid的列操作个人总结

来源:互联网 发布:sql添加一个人信息语句 编辑:程序博客网 时间:2024/05/17 02:01

DataGrid的列操作个人总结
绑定列
<asp:BoundColumn
DataField="datetime" 字段名
HeaderText="时间" 列表头
HeaderImagerUrl=""> 显示于列页眉中的图片,此图片会取代HeaderText的文本
</asp:BoundColumn>

如果“自动创建列”为true,则绑定列先显示,接着显示自动列,而且自动生成的列不会被加入到columns集合中。


超级链接列
<asp:HyperLinkColumn
Text="文本" //各列显示相同的文本,此时DataTextField优先
DataTextField="代码" //绑定的字段名
DataTextFormatString="" //来自定义DataTextField的显示格式

NavigateUrl="url" //所有列使用同一url
DataNavigateUrlField="codeId" //URL字段变量,即传递的变量值,有时和DataTextField同
DataNavigateUrlFormatString="WebForm2.aspx?code={0}" URL格式字符串,GET方式传递的字符串
Target="_blank" > //打开链接打开的位置或方式
</asp:HyperLinkColumn>

 

按钮列

<Columns>
普通按钮
<asp:ButtonColumn
Text="所有列统一按钮名" //所有列统一按钮名
DataTextField="持股名称" //绑定字段
CommandName="btn"> //
HeaderText="操作"> //列表头
</asp:ButtonColumn>

选择按钮
<asp:ButtonColumn
Text="选择"
DataTextField="持股名称"
CommandName="Select">
</asp:ButtonColumn>

编辑按钮
<asp:EditCommandColumn
ButtonType="LinkButton"
UpdateText="更新"
CancelText="取消"
EditText="编辑">
</asp:EditCommandColumn>

删除按钮
<asp:ButtonColumn
Text="删除"
ButtonType="PushButton"
CommandName="Delete">
</asp:ButtonColumn>

</Columns>

CommandName设置在DataGrid1_ItemCommand()事件中
获取同一行中哪个按钮被点击: string s=e.CommandName;

//默认是linkbutton,也必须是linkbutton


单击按钮首先响应 DataGrid1_ItemCommand 事件
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

TableRow tr=e.Item; //得到操作的当前行,存入控件
string code=tr.Cells[1].Text; //丛控件再得到单元格的文本
string time=tr.Cells[2].Text;

TableCell cell1=e.Item.Cells[1]; //这样也可以取得单元格的值,存入控件

Server.Transfer("WebForm2.aspx?code="+code+" &or time="+time);
}

接着不同按钮响应不同事件:
编辑
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
this.datashow();
}

更新
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//得到主键列的值
int i=(int)this.DataGrid1.DataKeys[e.Item.ItemIndex];
或 string ii=(string)this.DataGrid1.DataKeys[e.Item.ItemIndex];
根据主键,用单元格的数据更新主键对应的纪录
//写update语句

}

取消
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
this.datashow();
}

注意:可以将主键绑定列设为只读;

 

删除
//应首先设置DataKeyField属性为主键列
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

//得到主键列的值
int i=(int)this.DataGrid1.DataKeys[e.Item.ItemIndex];
或 string ii=(string)this.DataGrid1.DataKeys[e.Item.ItemIndex];
//写删除语句,

}


当在到服务器发送之间,在数据列表控件中选择不同的项时,引发 SelectedIndexChanged 事件
也可通过int i2 =(int)this.DataGrid1.DataKeys[this.DataGrid1.SelectedIndex];得到主键值

DataKeyField 是一个字段,他的所有键值内容被填入DataKeys集合中,通过DataKeys[]来去某条记录的主键值


排序

指定默认排序 :
选择“自动创建列”true;
在“行为”部分,选择“允许排序”框。
在SortCommand 事件里,通过e.SortExpression对视图重新排序绑定。
(缺点:每一列都有“链接”按钮,)

指定自定义排序:
选择“自动创建列”false;
在需要排序的列,设置SortExpression
注意:没有排序表达式的列将不引发 SortCommand 事件,所以先设置排序表达式

private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string sql="server=127.0.0.1;database=ltp;user id=sa;password=";
SqlConnection mycon=new SqlConnection(sql);

string selsql="select * from data";
SqlDataAdapter da=new SqlDataAdapter(selsql,mycon);

DataSet ds=new DataSet();
da.Fill(ds,"data");

DataView dv= new DataView(ds.Tables["data"]);

dv.Sort= e.SortExpression.ToString(); //设置排序关键字

this.DataGrid1.DataSource=dv; //重新绑定
this.DataGrid1.DataBind();


}

通过模版列来显示字段数据时,可以在模版列的<HeaderTemplate>加控件来执行排序。
按钮的CommandArgument设置成 排序表达式,(为了SortCommand 事件能取到)
按钮的CommandName="sort",务必小写

 

模版列

可以自行决定要在列中显示哪些控件(一个或多个),以及这些控件要绑定哪些字段。
加入模版列的按钮会将其click事件反升到DataGrid1_ItemCommand事件,但要判断CommandName
编辑模版/里、直接把控件拖进去即可。

注意 如果您调用了父控件(DataList、Repeater 或 DataGrid 控件)的 DataBind 方法,ItemCommand 事件将不会发生,原因是父控件的内容已经重置。因此,您通常不需要在每次往返时调用 DataBind 方法(即在初始化页时无需检查发回)。


<asp:TemplateColumn>

<HeaderTemplate>
<FONT face="宋体">模板列</FONT> //表头
</HeaderTemplate>

<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="D:/img/image/button_add.gif">
</asp:ImageButton>
</ItemTemplate>

<EditItemTemplate>
//这一行是当选择编辑按钮时,显示的控件或文本
</EditItemTemplate>
</asp:TemplateColumn>


得到模版列中控件的值(DataGrid1_ItemCommand事件中)
DropDownList listctr=(DropDownList)e.Item.FindControl("DropDownList2");
string s=listctr.SelectedValue;


CheckBox box=(CheckBox)e.Item.FindControl("CheckBox1");
bool b=box.Checked;
 

原创粉丝点击