How Do I Use a CheckBox to Update a Bit Type Data Field in a DataGrid

N

.NETn00b

I have only been programming for a few weeks, and I am struggling with
the DataGrid control.

My DataGrid is properly bound to the database. The problem that I am
having is that I don't know how to update. I'm not sure how to update
the correct record (and I'm also not sure of the Update query syntax:
all of my attempts were wrong).

My source looks like this:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="CheckBoxControl.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1"
name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<ASP:DATAGRID id=DataGrid1" runat="server"
AutoGenerateColumns="false" HeaderStyle-
CellSpacing="0" CellPadding="3" ShowFooter="false"
BorderColor="black" BackColor="#ccccff" Width="800"
OnItemDataBound="DataGrid1_ItemDataBound">
<Columns>
<asp:BoundColumn HeaderText="Order ID"
DataField="ord_id"/>
<asp:BoundColumn HeaderText="Last Name"
DataField="cust_lname" />
<asp:BoundColumn HeaderText="First Name"
DataField="cust_fname" />
<asp:BoundColumn HeaderText="address"
DataField="ord_shipto" />
<HeaderTemplate>
<input type="checkbox" id="checkAll" runat="server">
Shipped
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="shipped" AutoPostBack=True
runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASP:DATAGRID></form>
</body>
</HTML>

The Code Behind

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace CheckBoxControl
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
SqlConnection myConnection;

private void Page_Load(object sender, System.EventArgs e)
{
myConnection = new SqlConnection
("Server=localhost;uid=admin;pwd=test;database=orders");


if(!IsPostBack)
BindDataGrid();
}

public void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox chkBox = e.Item.FindControl("shipped")as
CheckBox;
DataRowView dRowView = (DataRowView)e.Item.DataItem;

if(cb != null && !dRowView.Row.IsNull("shipped"))
{
cb.Checked = (bool)dRowView["contract"];
}

}

public void BindDataGrid()
{
SqlDataAdapter dAdapter = new SqlDataAdapter ("SELECT * FROM
orders ", myConnection);
DataSet dataSet= new DataSet();
dAdapter.Fill(dataSet, "orders");
DataGrid1.DataSource = dataSet.Tables["orders"].DefaultView;
DataGrid1.DataBind();
}


#region Web Form Designer generated code

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
// Designer.

InitializeComponent();
base.OnInit(e);

}


/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler
(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}

#endregion
}

}

The field I want to update is "shipped" which is a SQL bit data type. If
a checkbox is checked (or unchecked) I want to update the shipped field
appropriately. (Again, not sure of the Update syntax or how to ensure
I'm updating the correct row.)

Any help would be very much appreciated!!!

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top