复选框 asp.net c# 网格
Checkbox asp.net c# Grid
我目前有一个数据网格
"SELECT * FROM table WHERE Hentet = 'nej';"
在那个网格下,我有一个完全相同的副本,但是
"WHERE Hentet = 'Ja';"
正如您将在代码中看到的那样。
两个网格都工作正常并获得了它们应该获得的数据,但是 "Grid 1" 的每一行都有一个复选框,因此您可以 Select 无论行,按下按钮并我希望值 Nej;
在该按钮上更改为 Ja;
单击,因此选中复选框的行将移动到 Grid2,现在它们的值为 = 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'。
对不起,如果我的问题很难理解。
泰卡
<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 的行数据绑定事件并隐藏该列。
我目前有一个数据网格
"SELECT * FROM table WHERE Hentet = 'nej';"
在那个网格下,我有一个完全相同的副本,但是
"WHERE Hentet = 'Ja';"
正如您将在代码中看到的那样。
两个网格都工作正常并获得了它们应该获得的数据,但是 "Grid 1" 的每一行都有一个复选框,因此您可以 Select 无论行,按下按钮并我希望值 Nej;
在该按钮上更改为 Ja;
单击,因此选中复选框的行将移动到 Grid2,现在它们的值为 = 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'。 对不起,如果我的问题很难理解。 泰卡
<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 的行数据绑定事件并隐藏该列。