how to add checkbox to a datagrid?

Discussion in 'ASP .Net Datagrid Control' started by Sedlin, May 16, 2005.

  1. Sedlin

    Sedlin Guest

    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());
    }
    }
    }
     
    Sedlin, May 16, 2005
    #1
    1. Advertising

  2. Sedlin

    Elton W Guest

    You can use

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

    HTH

    Elton Wang




    >-----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"
    >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());
    > }
    > }
    >}
    >
    >.
    >
     
    Elton W, May 16, 2005
    #2
    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. Frank Esser

    DataGrid: Add Checkbox

    Frank Esser, Mar 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,631
    =?Utf-8?B?S29zdGFkaW4gS29zdG92?=
    Mar 11, 2005
  2. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    844
    David Wier
    Oct 6, 2006
  3. Charles

    How Do I : Add a Click event to a checkbox in a DataGrid

    Charles, Jul 11, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    142
    Luke Zhang [MSFT]
    Jul 15, 2003
  4. Joey Pang
    Replies:
    4
    Views:
    537
    Joey Pang
    Jun 13, 2005
  5. J Gopinath

    how to add checkbox in DataGrid control dynamically?

    J Gopinath, Jun 1, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    217
    J Gopinath
    Jun 1, 2004
Loading...

Share This Page