读书人

有关于GridView的小疑点、老师

发布时间: 2012-01-02 22:40:04 作者: rapoo

有关于GridView的小问题、请教各位老师。
我在ASP页面中做了这样的操作详细请看代码相信您会明白我要做的是什么:

前台代码:

<table style="text-align: center; width: 100%">
<tr>
<td style="width: 100%; height: 64px; text-align: center;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="ID" ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
Width="100%">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" Visible="false" />
<asp:BoundField DataField="Fill_Time" HeaderText="日期" />
<asp:BoundField DataField="Goods_Num" HeaderText="配件、设备编号" />
<asp:BoundField DataField="Goods_Name" HeaderText="品名" />
<asp:BoundField DataField="Goods_Spce" HeaderText="规格" />
<asp:BoundField DataField="Goods_Amount" HeaderText="数量" />
<asp:BoundField DataField="Goods_Money" HeaderText="金额" />
<asp:BoundField DataField="Storage" HeaderText="使用明细、保管处" />
<asp:BoundField DataField="Treatment" HeaderText="处置明细" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:CheckBox ID="CheckBox1" runat="server" Text="经过财务" />
</td>
</tr>
</table>


后台代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Database db = new Database();
string sqlstr = "update Badness_Manage set Fill_Time='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Goods_Num='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Goods_Name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Goods_Spce='"


+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Goods_Amount='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',Goods_Money='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',Storage='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "',Treatment='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "' where ID='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
db.ExecuteSQL(sqlstr);
GridView1.EditIndex = -1;
this.Bind();
}
//绑定
private void Bind()
{
Database db = new Database();
string sqlstr = "select * from Badness_Manage where M_ID=" + SqlStringConstructor.GetQuotedString(Request["Id"]);
DataTable dt = db.GetDataTable(sqlstr);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Database db = new Database();
string sqlstr = "delete from Badness_Manage where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
db.ExecuteSQL(sqlstr);
this.Bind();

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.Bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.Bind();
}

现在有一个小小的问题就是说,当我在运行程序的时候会出现的,问题是:

1、当我点击修改的时候会出现页面布局变大不好控制,我想问问看看有什么办法避免这样的问题呢?BoundField中也没有属性控制其大小郁闷啊请各位前辈给予帮助

[解决办法]
具体是怎么个大法。
下面这句代码本身就是string,不需要在加上tostring(),代码会简洁一点点。
(TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text

[解决办法]
哈哈 写了一大堆代码 最后的问题是"BoundField中有没有属性控制其大小"
好像跟BoundField没什么直接关系吧 是你里面的控件把它撑大的(我是这样认为的)
css应该可以控制

读书人网 >asp.net

热点推荐