Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Using a CheckBox in a DataGrid to Update a Bit Type Data Field

Reply
Thread Tools

Using a CheckBox in a DataGrid to Update a Bit Type Data Field

 
 
Nu2ASP.NET
Guest
Posts: n/a
 
      11-17-2005
What I am trying to do is essentially 'flip' the bits, when the user
clicks in the checkbox. For example, if the CheckBox appears checked,
and the user un-checks it, I want the underlying data field to change
from a "1" to a "0" (and vica versa).

I have the bindings worked out, I just can't figure out how to do the
update.

Here is my source:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="CheckBoxTest.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">
<ASPATAGRID id="MyDataGrid" runat="server"
AutoGenerateColumns="false"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
Font-Name="Verdana"
CellSpacing="0" CellPadding="3" ShowFooter="false"
BorderColor="black"
BackColor="#ccccff" Width="800"
OnItemDataBound="MyDataGrid_ItemDataBound">
<Columns>
<asp:BoundColumn HeaderText="au_id" DataField="au_id"/>
<asp:BoundColumn HeaderText="au_lname"
DataField="au_lname" />
<asp:TemplateColumn HeaderText="au_fname">
<ItemTemplate>
<asp:Label id="au_fname" Text='<%#
DataBinder.Eval(Container.DataItem,
"au_fname") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="city" DataField="city" />
<asp:TemplateColumn HeaderText="contract">
<HeaderTemplate>
<input type="checkbox" id="checkAll" runat="server">
Contracts
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="contract" AutoPostBack=True
runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASPATAGRID></form>
</body>
</HTML>

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 CheckBoxTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>

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



private void Page_Load(object sender, System.EventArgs e)
{
myConnection = new SqlConnection

("Server=localhost;uid=sa;pwd=preview;database=pub s");


if(!IsPostBack)
BindGrid();
}

public void MyDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox cb = e.Item.FindControl("contract")as CheckBox;
DataRowView drv = (DataRowView)e.Item.DataItem;

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

}


public void BindGrid()
{
SqlDataAdapter dAdapter = new SqlDataAdapter ("SELECT * FROM
authors ",
myConnection);
DataSet dSet= new DataSet();
dAdapter.Fill(dSet, "authors");
MyDataGrid.DataSource = dSet.Tables["authors"].DefaultView;
MyDataGrid.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.MyDataGrid.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler
(this.MyDataGrid_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}

#endregion
}

}

I am using a SQL 2000 database (note I'm using the sample pubs database
above).

As always, help is greatly appreciated!!!

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      11-17-2005
add an event handler to your checkbox: OnCheckedChanged ="MyFunction" and in
the method that handles the event do the opposite of what you did in the
MyDataGrid_ItemDataBound (i.e. drv["contract"]=cb.Checked

I have a complete sample code for a similar usage (to delete instead of
update):http://www.societopia.net/Samples/Da...olsEvents.aspx
--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"Nu2ASP.NET" wrote:

> What I am trying to do is essentially 'flip' the bits, when the user
> clicks in the checkbox. For example, if the CheckBox appears checked,
> and the user un-checks it, I want the underlying data field to change
> from a "1" to a "0" (and vica versa).
>
> I have the bindings worked out, I just can't figure out how to do the
> update.
>
> Here is my source:
>
> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
> AutoEventWireup="false" Inherits="CheckBoxTest.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">
> <ASPATAGRID id="MyDataGrid" runat="server"
> AutoGenerateColumns="false"
> HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
> Font-Name="Verdana"
> CellSpacing="0" CellPadding="3" ShowFooter="false"
> BorderColor="black"
> BackColor="#ccccff" Width="800"
> OnItemDataBound="MyDataGrid_ItemDataBound">
> <Columns>
> <asp:BoundColumn HeaderText="au_id" DataField="au_id"/>
> <asp:BoundColumn HeaderText="au_lname"
> DataField="au_lname" />
> <asp:TemplateColumn HeaderText="au_fname">
> <ItemTemplate>
> <asp:Label id="au_fname" Text='<%#
> DataBinder.Eval(Container.DataItem,
> "au_fname") %>' runat="server" />
> </ItemTemplate>
> </asp:TemplateColumn>
> <asp:BoundColumn HeaderText="city" DataField="city" />
> <asp:TemplateColumn HeaderText="contract">
> <HeaderTemplate>
> <input type="checkbox" id="checkAll" runat="server">
> Contracts
> </HeaderTemplate>
> <ItemTemplate>
> <asp:CheckBox id="contract" AutoPostBack=True
> runat="server"></asp:CheckBox>
> </ItemTemplate>
> </asp:TemplateColumn>
> </Columns>
> </ASPATAGRID></form>
> </body>
> </HTML>
>
> 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 CheckBoxTest
> {
> /// <summary>
> /// Summary description for WebForm1.
> /// </summary>
>
> public class WebForm1 : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.DataGrid MyDataGrid;
> SqlConnection myConnection;
>
>
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> myConnection = new SqlConnection
>
> ("Server=localhost;uid=sa;pwd=preview;database=pub s");
>
>
> if(!IsPostBack)
> BindGrid();
> }
>
> public void MyDataGrid_ItemDataBound(object sender,
> System.Web.UI.WebControls.DataGridItemEventArgs e)
> {
> if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> {
> CheckBox cb = e.Item.FindControl("contract")as CheckBox;
> DataRowView drv = (DataRowView)e.Item.DataItem;
>
> if(cb != null && !drv.Row.IsNull("contract"))
> {
> cb.Checked = (bool)drv["contract"];
> }
> }
>
> }
>
>
> public void BindGrid()
> {
> SqlDataAdapter dAdapter = new SqlDataAdapter ("SELECT * FROM
> authors ",
> myConnection);
> DataSet dSet= new DataSet();
> dAdapter.Fill(dSet, "authors");
> MyDataGrid.DataSource = dSet.Tables["authors"].DefaultView;
> MyDataGrid.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.MyDataGrid.ItemDataBound += new
> System.Web.UI.WebControls.DataGridItemEventHandler
> (this.MyDataGrid_ItemDataBound);
> this.Load += new System.EventHandler(this.Page_Load);
> }
>
> #endregion
> }
>
> }
>
> I am using a SQL 2000 database (note I'm using the sample pubs database
> above).
>
> As always, help is greatly appreciated!!!
>
>

 
Reply With Quote
 
 
 
 
Nu2ASP.NET
Guest
Posts: n/a
 
      11-18-2005
Thank You Phillip!

Your example is a very good one: I am, however, having trouble with the
syntax for the update query. I am trying to update the contract field
(bit data type) in the pubs database. I made several attempts, using the
SQL Query analyzer, but couldn't get the syntax right. Could you
possibly provide me with a sample? I would really appreciate it.



--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writeable checkbox in datagrid from bit field in table hazz ASP .Net 4 04-27-2006 08:15 PM
How Do I Use a CheckBox to Update a Bit Type Data Field in a DataGrid .NETn00b ASP .Net Web Controls 0 11-19-2005 04:48 PM
Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked. Joey Pang ASP .Net Datagrid Control 4 06-13-2005 02:29 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments