复选框asp.net c#网格

本文关键字:网格 net asp 复选框 | 更新日期: 2023-09-27 17:50:17

我目前有一个

"SELECT * FROM table WHERE Hentet = 'nej';"

在这个网格下,我有一个确切的副本,但

"WHERE Hentet = 'Ja';"

正如您将在代码中看到的那样。

这两个网格都很好地工作,并获得了它们应该获得的数据,但"网格1"的每一行都有一个复选框,所以你可以选择任何行,按下按钮,我希望该按钮上的值Nej;更改为Ja;单击,这样复选框被选中的行将被移到网格2,现在它们的值为=Ja。Grid2没有复选框列,所以它只是从grid1到Grid2,而不是双向的。

网格1

    <asp:DataGrid ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false">
           <Columns>
           <asp:TemplateColumn>
               <ItemTemplate>
               <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox>
                   </ItemTemplate>
           </asp:TemplateColumn>
           <asp:BoundColumn DataField="dato" HeaderText="Dato">
            </asp:BoundColumn>
            <asp:BoundColumn DataField="Antal" HeaderText="Antal">
            </asp:BoundColumn>
           <asp:Boundcolumn HeaderText="Navn" Datafield="VareNAvn">
            </asp:Boundcolumn>
       <asp:BoundColumn DataField="KøbtAfBrugerID" HeaderText="Købt af ID">
            </asp:BoundColumn>
            <asp:Boundcolumn HeaderText="Hented" DataField="Afhented">
                </asp:Boundcolumn>
       </Columns>
    </asp:DataGrid>
  <asp:Button ID="Button_Hented" CssClass="btfarve" runat="server" Text="Afhentet" OnClick="Button_Hented_Click" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT * FROM [Transactioner] WHERE afhented ='Nej';">
    </asp:SqlDataSource>

网格2

<asp:DataGrid ID="GridView2" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="false">
           <Columns>
               <asp:BoundColumn DataField="dato" HeaderText="Dato">
                </asp:BoundColumn>
                <asp:BoundColumn DataField="Antal" HeaderText="Antal">
                </asp:BoundColumn>
               <asp:Boundcolumn HeaderText="Navn" Datafield="VareNAvn">
                </asp:Boundcolumn>
            <asp:BoundColumn DataField="KøbtAfBrugerID" HeaderText="Købt af ID">
                </asp:BoundColumn>
                <asp:Boundcolumn HeaderText="Hented" DataField="Afhented">
                    </asp:Boundcolumn>
           </Columns>
        </asp:DataGrid>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
        SelectCommand="SELECT * FROM [Transactioner] WHERE afhented ='Ja';">
        </asp:SqlDataSource>

代码隐藏

protected void Button_Hented_Click(object sender, EventArgs e)
     {
         string Hejsa;
         SqlConnection conn = new SqlConnection();
         conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
         SqlCommand cmd = new SqlCommand();
         cmd.Connection = conn;
         cmd.CommandText = "SELECT * FROM Transactioner";

         conn.Open();
         SqlDataReader reader = cmd.ExecuteReader();
         if (reader.Read())
         {
             Hejsa = reader["Id"].ToString();

             foreach (DataGridItem item in GridView1.Items)
             {
                 CheckBox Cb = item.Cells[0].Controls[1] as CheckBox;

                 if (Cb.Checked)
                 {
                     SqlConnection conn2 = new SqlConnection();
                     conn2.ConnectionString =
                         ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
                     SqlCommand cmd2 = new SqlCommand();
                     cmd2.Connection = conn2;
                     cmd2.CommandText = "UPDATE Transactioner"
                         + " SET Afhented = @Afhented"
                         + " where Id = @Id";

                     cmd2.Parameters.Add("@Afhented", SqlDbType.NVarChar).Value = "Ja";
                     cmd2.Parameters.Add("@Id", SqlDbType.Int).Value = Hejsa;

                     conn2.Open();
                     cmd2.ExecuteNonQuery();
                     conn2.Close();
                 }
             }
             Response.Redirect(Request.RawUrl);
         }
         conn.Close();
        }

所以这就是所有的代码,我没有收到任何错误消息,我真的看不出又出了什么问题,我只想让代码点击按钮,如果复选框被选中,将值"Nej"更改为"Ja"。如果我的问题很难理解,我很抱歉。Tekar

复选框asp.net c#网格

<asp:DataGrid ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false">
           <Columns>
           <asp:TemplateColumn>
               <ItemTemplate>
               <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox>
                   </ItemTemplate>
           </asp:TemplateColumn>
<asp:BoundColumn DataField="Id" HeaderText="Id"> <!--New Column Added-->
           <asp:BoundColumn DataField="dato" HeaderText="Dato">
            </asp:BoundColumn>
            <asp:BoundColumn DataField="Antal" HeaderText="Antal">
            </asp:BoundColumn>
           <asp:Boundcolumn HeaderText="Navn" Datafield="VareNAvn">
            </asp:Boundcolumn>
       <asp:BoundColumn DataField="KøbtAfBrugerID" HeaderText="Købt af ID">
            </asp:BoundColumn>
            <asp:Boundcolumn HeaderText="Hented" DataField="Afhented">
                </asp:Boundcolumn>
       </Columns>
    </asp:DataGrid>
  <asp:Button ID="Button_Hented" CssClass="btfarve" runat="server" Text="Afhentet" OnClick="Button_Hented_Click" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
    SelectCommand="SELECT * FROM [Transactioner] WHERE afhented ='Nej';">
    </asp:SqlDataSource>

在您的按钮点击事件中,找到您想要更新的记录的id

    protected void Button_Hented_Click(object sender, EventArgs e)
         {
             string Hejsa;
    // No need to keep on recreating these objects in a loop. 
    SqlConnection conn2 = new SqlConnection();
                         conn2.ConnectionString =
                             ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
                         SqlCommand cmd2 = new SqlCommand();
                         cmd2.Connection = conn2;
    conn2.Open();
                 foreach (DataGridItem item in GridView1.Items)
                 {
                     CheckBox Cb = item.Cells[0].Controls[1] as CheckBox;

                     if (Cb.Checked)
                     {
                         Hesja = item.Cells[1].Text.Trim(); // Get the id of checked record

                         cmd2.CommandText = "UPDATE Transactioner"
                             + " SET Afhented = @Afhented"
                             + " where Id = @Id";

                         cmd2.Parameters.Add("@Afhented", SqlDbType.NVarChar).Value = "Ja";
                         cmd2.Parameters.Add("@Id", SqlDbType.Int).Value = Hejsa;

                         cmd2.ExecuteNonQuery();

                     }
                 }
 conn2.Close();
                 Response.Redirect(Request.RawUrl);

            }

如果您想从Gridview中隐藏新添加的Id列,请通过实现Gridview的行数据绑定事件并隐藏该列来实现。