在对GridView进行操作时,经常要取一列或者多列的值,比如 点击某一个行的编辑按钮, 对该行数据进行修改。
取数据主键的方法: 比如主键为: DataKeyNames="book_class_id"
后台可以这样取: this.GV_borrow.DataKeys[1].Value.ToString()
不同用法的GridView的取值方法也不相同。
用法一:
<asp:GridView ID="GVStud" runat="server" DataKeyNames="book_class_id" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" PageSize="8" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GVStud_PageIndexChanging" OnRowEditing="GVStud_RowEditing" OnRowUpdating="GVStud_RowUpdating" OnRowDeleting="GVStud_RowDeleting" OnRowCancelingEdit="GVStud_RowCancelingEdit" Width="650px" > <PagerSettings FirstPageText="第一页" LastPageText="最后页" NextPageText="下一页" PreviousPageText="前一页" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="book_class_id" HeaderText="编号" ReadOnly="True" /> <asp:TemplateField HeaderText="类名"> <ItemTemplate> <%# Eval("book_class_name")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="nameEdit" Text='<%# Eval("book_class_name") %>' runat="server" Width="140px" /> </EditItemTemplate> <ItemStyle Width="150px" /> </asp:TemplateField> <asp:CommandField HeaderText="操作" ShowEditButton="True" ButtonType="Button" UpdateText="修改" > <ItemStyle Width="100px"></ItemStyle> </asp:CommandField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="删除" OnClientClick="return confirm('您确认删除该记录吗?');" CausesValidation="False" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <EditRowStyle BackColor="#999999" /> <RowStyle HorizontalAlign="Center" BackColor="#F7F6F3" ForeColor="#333333" Height="20px" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView>
后台事件代码:
public void BindStud() { string type = "type"; string sql = "select * from book_class order by book_class_id "; DataSet ds = new DataSet(); ds = CC.GetDataSet(sql, type); GVStud.DataSource = ds.Tables[0]; GVStud.DataBind(); } protected void GVStud_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GVStud.PageIndex = e.NewPageIndex; BindStud(); } protected void GVStud_RowEditing(object sender, GridViewEditEventArgs e) { GVStud.EditIndex = e.NewEditIndex; BindStud(); } protected void GVStud_RowUpdating(object sender, GridViewUpdateEventArgs e) { } protected void GVStud_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GVStud.EditIndex = -1; BindStud(); } protected void GVStud_RowDeleting(object sender, GridViewDeleteEventArgs e) {}
主键内容值的提取方法:
String id = GVStud.DataKeys[e.RowIndex].Value.ToString();
对应编辑列的值的提取方法:
string type_title = ((TextBox)GVStud.Rows[e.RowIndex].FindControl("nameEdit")).Text.ToString();
<asp:GridView ID="GV_borrow" runat="server" DataKeyNames="borrow_id" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GV_RowCommand" Width="650px" > <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="borrow_id" HeaderText="编号" /> <asp:BoundField DataField="book_id" HeaderText="书编号" /> <asp:BoundField DataField="book_name" HeaderText="书名" /> <asp:BoundField DataField="user_id" HeaderText="借书人" /> <asp:BoundField DataField="value" HeaderText="是否续借" /> <asp:BoundField DataField="borrow_time" HeaderText="借书时间" /> <asp:BoundField DataField="return_time" HeaderText="应还时间" /> <asp:BoundField DataField="borrow_state" HeaderText="状态" /> <asp:BoundField DataField="owe_money" HeaderText="欠款" /> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="update" OnClientClick="showUpdate()" runat="server" CausesValidation="false" CommandName="up" CommandArgument='<%# bind("book_id") %>' Text="还书"> </asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <RowStyle ForeColor="#000066" /> <FooterStyle BackColor="White" ForeColor="#000066" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>
<asp:ImageButton ID="editImageButton" runat="server" ImageUrl="~/images/bt_edit.gif" CommandArgument='<%#Eval("dict_id")+","+Eval("dict_type")%>' onclick="editImageButton_Click" Height="20" Width="20" />
object[] arg=e.CommandArgument.ToString().Split(','); //注意是单引号 string arg0=arg[0].ToString(); string arg1=arg[1].ToString();
在绑定数据时还可以用后台的函数先进行处理:
如下:
<ItemTemplate> 登录名 : <%# Eval("login_name")%> <br /> <br /> 真实姓名 : <%# Eval("real_name")%> <br /> <br /> 管理员类别 :<%# getClassName1(DataBinder.Eval(Container.DataItem,"dbo_type").ToString()) %>
public string getClassName1(string winery_id) { string type = "dbo_login_type"; string sql = "select dbo_name from dbo_login_type where dbo_type='" + winery_id + "'"; DataSet ds = new DataSet(); ds = CC.GetDataSet(sql, type); string ClassName = ""; ClassName = ds.Tables[0].Rows[0][0].ToString(); return ClassName; }