gridview定义的dropdownlist控件怎么绑定到字段<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataKeyNames="roomno" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField HeaderText="房间号" DataField="roomno" />
<asp:TemplateField HeaderText="房间类型" > <%--//如何绑定到字段--%>
<ItemTemplate>
<asp:DropDownList ID="roomtype" runat="server" >
<asp:ListItem Value="1">豪华房</asp:ListItem>
<asp:ListItem Value="2">商务房</asp:ListItem>
<asp:ListItem Value="3">双人房</asp:ListItem>
<asp:ListItem Value="4">房型4</asp:ListItem>
<asp:ListItem Value="5">房型5</asp:ListItem>
<asp:ListItem Value="6">房型6</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roomstatu" HeaderText="房间状态" />
<asp:BoundField HeaderText="房价" DataField="unitprice" DataFormatString="{0:c}" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>.cs
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
int index = e.RowIndex;//当前选择行索引值
string roomno = GridView1.DataKeys[index].Value.ToString();//获取主键房间号
string roomtype = ((TextBox)(GridView1.Rows[index].Cells[1].Controls[0])).Text.Trim();
string roomstatu = ((TextBox)(GridView1.Rows[index].Cells[2].Controls[0])).Text.Trim();
string unitprice = ((TextBox)(GridView1.Rows[index].Cells[3].Controls[0])).Text.Trim(); string sql = "update room set roomtype="+roomtype+",roomstatu="+roomstatu+",unitprice="+unitprice+"";//更新语句
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
int ret = comm.ExecuteNonQuery();
if (ret>0)
{
//更新成功
}
else
{
//更新失败
}
GridView1.EditIndex = -1;
DataBind();
}
catch (Exception ex)
{
throw;
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = e.RowIndex;
string roomno = GridView1.DataKeys[index].Value.ToString();
string sql = "delete from room where roomno="+roomno+"";//删除语句
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
conn.Close();
DataBind();
}
<Columns>
<asp:BoundField HeaderText="房间号" DataField="roomno" />
<asp:TemplateField HeaderText="房间类型" > <%--//如何绑定到字段--%>
<ItemTemplate>
<asp:DropDownList ID="roomtype" runat="server" >
<asp:ListItem Value="1">豪华房</asp:ListItem>
<asp:ListItem Value="2">商务房</asp:ListItem>
<asp:ListItem Value="3">双人房</asp:ListItem>
<asp:ListItem Value="4">房型4</asp:ListItem>
<asp:ListItem Value="5">房型5</asp:ListItem>
<asp:ListItem Value="6">房型6</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roomstatu" HeaderText="房间状态" />
<asp:BoundField HeaderText="房价" DataField="unitprice" DataFormatString="{0:c}" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>.cs
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
int index = e.RowIndex;//当前选择行索引值
string roomno = GridView1.DataKeys[index].Value.ToString();//获取主键房间号
string roomtype = ((TextBox)(GridView1.Rows[index].Cells[1].Controls[0])).Text.Trim();
string roomstatu = ((TextBox)(GridView1.Rows[index].Cells[2].Controls[0])).Text.Trim();
string unitprice = ((TextBox)(GridView1.Rows[index].Cells[3].Controls[0])).Text.Trim(); string sql = "update room set roomtype="+roomtype+",roomstatu="+roomstatu+",unitprice="+unitprice+"";//更新语句
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
int ret = comm.ExecuteNonQuery();
if (ret>0)
{
//更新成功
}
else
{
//更新失败
}
GridView1.EditIndex = -1;
DataBind();
}
catch (Exception ex)
{
throw;
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = e.RowIndex;
string roomno = GridView1.DataKeys[index].Value.ToString();
string sql = "delete from room where roomno="+roomno+"";//删除语句
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
conn.Close();
DataBind();
}
解决方案 »
- 如何在asp.net中隐藏网页关键字啊
- 问几个基础问题..望详解
- .NET中连接数据出现ADO错误:OX80004005怎么解决呀!急呀!
- 关于客户端如何调用其他exe文件?
- 在Access中的查询语句,下面的语句有什么问题
- 在asp.net 2.0,怎样设置文件属性的生成操作?
- 如题,一个有一年半工作经验的asp.net程序员,水平不是很高的,中等吧.熟练开发asp.net应用程序. 大概在什么价位?谢谢. 地区是:成都
- 哈哈,笑死我了!!! 最近攻击了这个网站,大家看看效果!
- 初学 求各位大哥帮忙!关于DataGrid的问题 <在线=:》
- 大家帮我看看bytes2BSTR这个函数具体有什么用?在线等
- 使于输入密码的textBox中显示默认提示
- 页面刷新 空白页
试试