I have the following code in my code behind page:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
namespace CheckBoxTest
{
/// <summary>
/// Summary description for WebForm1.
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data Grid MyDataGrid;
SqlConnection myConnection;
private void Page_Load(objec t sender, System.EventArg s e)
{
myConnection = new SqlConnection
("Server=localh ost;uid=sa;pwd= preview;databas e=pubs");
if(!IsPostBack)
BindGrid();
}
private void MyDataGrid_Item DataBound(objec t sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
CheckBox cb = e.Item.FindCont rol("contract") as CheckBox;
if(cb != null)
{
if(DataBinder.E val(e.Item.Data Item,
"contract")!=DB Null.Value)
{
if(Convert.ToBo olean(DataBinde r.Eval(e.Item.D ataItem,
"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(d Set, "authors");
MyDataGrid.Data Source =
dSet.Tables["authors"].DefaultView;
MyDataGrid.Data Bind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.MyDataGrid .ItemDataBound += new
System.Web.UI.W ebControls.Data GridItemEventHa ndler
(this.MyDataGri d_ItemDataBound );
this.Load += new System.EventHan dler(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
InitializeCompo nent, since that's where it seemed to make the most
sense.
Am I missing something *really* obviously wrong here?
Thanks!!!