how to add checkbox to a datagrid?

S

Sedlin

Hello,
I am trying to add a checkbox to a datagrid. But i dont want to bind a
data with the checkBox. I only want to know if any user has chacked
the checkbox!

How can I do that?

You can try my problem here: :http://das.aspweb.cz/WebForm1.aspx



<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="datagrid.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" style="Z-INDEX:
101; LEFT: 200px; POSITION: absolute; TOP: 120px"
runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="myItem"
HeaderText="myColumn1"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox
Runat="server" ID="check"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn
Text="Click"></asp:ButtonColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>


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 datagrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid
DataGrid1;
protected DataTable dt;

private void Page_Load(object sender, System.EventArgs
e)
{

string[] array = {"Item1", "Item2"};
dt = new DataTable();
dt.Columns.Add("myItem", typeof(string));
for(int i=0; i<20; i++)
dt.Rows.Add(new object[]
{"myLine"+i});
DataGrid1.DataSource = dt;
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.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.Load += new
System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells[0].Text + ":
checked-");

Response.Write(((CheckBox)e.Item.FindControl("check")).Checked.ToString());
}
}
}
 
E

Elton W

You can use

<asp:CheckBox
Runat="server" ID="check" Checked =<%# DataBinder.Eval
(Container.DataItem, "DataFieldName") %> ></asp:CheckBox>

HTH

Elton Wang
(e-mail address removed)


-----Original Message-----
Hello,
I am trying to add a checkbox to a datagrid. But i dont want to bind a
data with the checkBox. I only want to know if any user has chacked
the checkbox!

How can I do that?

You can try my problem
here: :http://das.aspweb.cz/WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="datagrid.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" style="Z-INDEX:
101; LEFT: 200px; POSITION: absolute; TOP: 120px"
runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="myItem"


<asp:CheckBox
Runat="server" ID="check"></asp:CheckBox>
<asp:ButtonColumn
Text="Click"></asp:ButtonColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>


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 datagrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid
DataGrid1;
protected DataTable dt;

private void Page_Load(object sender, System.EventArgs
e)
{

string[] array = {"Item1", "Item2"};
dt = new DataTable();
dt.Columns.Add("myItem", typeof (string));
for(int i=0; i<20; i++)
dt.Rows.Add(new object[]
{"myLine"+i});
DataGrid1.DataSource = dt;
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.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_ItemCommand);
this.Load += new
System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells [0].Text + ":
checked-");

Response.Write(((CheckBox)e.Item.FindControl ("check")).Checked.ToString());
}
}
}

.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top