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

Discussion in 'ASP .Net Web Controls' started by .NETn00b, Nov 19, 2005.

  1. .NETn00b

    .NETn00b Guest

    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!


    --
    Sent via .NET Newsgroups
    http://www.dotnetnewsgroups.com
    .NETn00b, Nov 19, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nu2ASP.NET
    Replies:
    2
    Views:
    648
    Nu2ASP.NET
    Nov 18, 2005
  2. hazz
    Replies:
    4
    Views:
    3,867
  3. Replies:
    3
    Views:
    1,720
    Timothy Bendfelt
    Jan 19, 2007
  4. akineko
    Replies:
    7
    Views:
    1,306
    akineko
    Dec 22, 2008
  5. Joey Pang
    Replies:
    4
    Views:
    494
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page