Reputation: 93
I currently have a Data Grid that
"SELECT * FROM table WHERE Hentet = 'nej';"
under that grid I have a exact copy of it, but
"WHERE Hentet = 'Ja';"
as you will see in the code.
Both grid's work fine and get the data they are supposed to get, however there is a check box on each row of "Grid 1", so you can Select Whatever Rows, Press the button and I want the value Nej;
to change to Ja;
on that button Click, so the rows with a checked check box will be moved to Grid2, now that their value is = Ja
. Grid2 does not have a check box column, so its only from grid1 to grid2, and not both ways.
Grid 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>
Grid2
<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>
Code Behind
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();
}
So this is all the code, I get NO error messages atall and i really cant see what is wrong again, All i really want the code to do is on button click, if checkbox is checked, change the value 'Nej' to 'Ja'. Sorry if my question was hard to understand. Tekar
Upvotes: 0
Views: 489
Reputation: 1275
<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>
In your button Click event find what is the id of record you want to update
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);
}
If you want to hide the newly added Id column from Gridview do so by implementing row databound event of the gridview and hide that column.
Upvotes: 1