I currently have a DataGrid 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 checkbox 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 checkbox will be moved to Grid2, now that their value is = 'Ja'. Grid2 does not have a checkbox 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
Aucun commentaire:
Enregistrer un commentaire