GridView1控件批量更改数据信息,该如何处理
GridView1控件批量更改数据信息

查询出相关信息后,勾选第二跳信息,然后点击报废按钮,将编号为04-0001的一体机在数据表tb_equipment中的isactive列的值从1变为0(就是使用状态改为报废的意思)
前台代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BorderColor="#000066"
CellPadding="4" ForeColor="#333333" GridLines="Vertical" PageSize="5"
DataKeyNames="number" EnableViewState="False" >
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox id="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="number" HeaderText="自编码" />
<asp:BoundField DataField="name" HeaderText="设备名称" />
<asp:BoundField DataField="kind" HeaderText="品牌" />
<asp:BoundField DataField="peizhi" HeaderText="配置" />
<asp:BoundField DataField="company" HeaderText="单位" />
<asp:BoundField DataField="department" HeaderText="部门" />
<asp:BoundField DataField="round" HeaderText="线路" />
<asp:BoundField DataField="useplace" HeaderText="使用地点" />
<asp:BoundField DataField="usetime" HeaderText="使用日期" />
<asp:HyperLinkField HeaderText="分配"
Text="调整" DataNavigateUrlFields="number"
DataNavigateUrlFormatString="~/设备领用/调整/change.aspx?number={0}" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
CS代码:
protected void Button4_Click(object sender, EventArgs e)
{
string[] n =new string[5];
string pl;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
pl = GridView1.DataKeys[i].Value.ToString();
string sqlText = "update tb_equipment set isactive = 0 where isactive=1 and number='"+pl+"'";
z.ExecSql(sqlText);
}
}
}
我的本意是点击”报废“按钮,循环遍历GridView1控件中的所有行,找出其中 CheckBox勾选的行信息,每一行的自编码,对应数据表中的number字段,是唯一确定各个设备的。isactive为1表示设备可以使用,为0则表示报废。请大家看下代码该怎么写
------解决思路----------------------
你试一下,应该没有什么问题
查询出相关信息后,勾选第二跳信息,然后点击报废按钮,将编号为04-0001的一体机在数据表tb_equipment中的isactive列的值从1变为0(就是使用状态改为报废的意思)
前台代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BorderColor="#000066"
CellPadding="4" ForeColor="#333333" GridLines="Vertical" PageSize="5"
DataKeyNames="number" EnableViewState="False" >
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox id="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="number" HeaderText="自编码" />
<asp:BoundField DataField="name" HeaderText="设备名称" />
<asp:BoundField DataField="kind" HeaderText="品牌" />
<asp:BoundField DataField="peizhi" HeaderText="配置" />
<asp:BoundField DataField="company" HeaderText="单位" />
<asp:BoundField DataField="department" HeaderText="部门" />
<asp:BoundField DataField="round" HeaderText="线路" />
<asp:BoundField DataField="useplace" HeaderText="使用地点" />
<asp:BoundField DataField="usetime" HeaderText="使用日期" />
<asp:HyperLinkField HeaderText="分配"
Text="调整" DataNavigateUrlFields="number"
DataNavigateUrlFormatString="~/设备领用/调整/change.aspx?number={0}" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
CS代码:
protected void Button4_Click(object sender, EventArgs e)
{
string[] n =new string[5];
string pl;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
pl = GridView1.DataKeys[i].Value.ToString();
string sqlText = "update tb_equipment set isactive = 0 where isactive=1 and number='"+pl+"'";
z.ExecSql(sqlText);
}
}
}
我的本意是点击”报废“按钮,循环遍历GridView1控件中的所有行,找出其中 CheckBox勾选的行信息,每一行的自编码,对应数据表中的number字段,是唯一确定各个设备的。isactive为1表示设备可以使用,为0则表示报废。请大家看下代码该怎么写
------解决思路----------------------
string pl="";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
pl += "'"+GridView1.DataKeys[i].Value.ToString()+"',";
}
}
if(pl!="")
{
pl = pl.substr(0,pl.length-1);
}
string sqlText = "update tb_equipment set isactive = 0 where isactive=1 and number in ("+pl+")";
z.ExecSql(sqlText);
你试一下,应该没有什么问题