Cannot Bind Check Box in DataGrid

N

Nu2ASP.NET

I have the following code in my code behind page:

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.
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=pubs");

if(!IsPostBack)
BindGrid();
}

private void MyDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
CheckBox cb = e.Item.FindControl("contract") as CheckBox;

if(cb != null)
{
if(DataBinder.Eval(e.Item.DataItem,
"contract")!=DBNull.Value)
{
if(Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem,
"contract")) )
cb.Checked = true;
else
cb.Checked = false;
}
}
}

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


}
}

The problem I'm having is that the checkbox is not properly reflecting
the state of the underlying data field, which, in this case, is an SQL
2000 bit type. All of the checkboxes appear unchecked. (I want the
check box to appear checked if the underlying data field contains a "1"
and unchecked if it contains a "0".)

I added the event handler for the ItemDataBound event to
InitializeComponent, since that's where it seemed to make the most
sense.

Am I missing something *really* obviously wrong here?

Thanks!!!
 
G

Guest

Try following in MyDataGrid_ItemDataBound

if(e.Item.Itemtype == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox cb = (CheckBox)e.Item.FindControl("contract");
DataRowView drv = (DataRowView)e.Item.DataItem;
if(cb != null && !drv.Row.IsNull("contract") )
{
cb.Checked = (bool)drv["contract"];

}
}


HTH

Elton Wang


Nu2ASP.NET said:
I have the following code in my code behind page:

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.
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=pubs");

if(!IsPostBack)
BindGrid();
}

private void MyDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
CheckBox cb = e.Item.FindControl("contract") as CheckBox;

if(cb != null)
{
if(DataBinder.Eval(e.Item.DataItem,
"contract")!=DBNull.Value)
{
if(Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem,
"contract")) )
cb.Checked = true;
else
cb.Checked = false;
}
}
}

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


}
}

The problem I'm having is that the checkbox is not properly reflecting
the state of the underlying data field, which, in this case, is an SQL
2000 bit type. All of the checkboxes appear unchecked. (I want the
check box to appear checked if the underlying data field contains a "1"
and unchecked if it contains a "0".)

I added the event handler for the ItemDataBound event to
InitializeComponent, since that's where it seemed to make the most
sense.

Am I missing something *really* obviously wrong here?

Thanks!!!
 
N

Nu2ASP.NET

Thank you everyone!!!

I have since resolved the issue, but I couldn't have done it without
your help and suggestions.

I really appreciate your input!
 
N

Nu2ASP.NET

Thank You!

I solved the issue, and now have everthing binding as it should.

Here's my solution (in case anyone is curious):

<%@ 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">
<ASP:DATAGRID 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>
</ASP:DATAGRID></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=pubs");


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
}

}

Thanks again to everyone for their help and suggestions. You were all a
great help.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top