how to add checkbox to a datagrid?

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default how to add checkbox to a datagrid?

    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: :[url]http://das.aspweb.cz/WebForm1.aspx[/url]



    <%@ 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.DataGridCommandEventHand ler(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("chec k")).Checked.ToString());
    }
    }
    }

    Sedlin Guest

  2. Similar Questions and Discussions

    1. CheckBox in Datagrid
      Hello can anyone help me for displaying a checkbox into datagrid. thanks
    2. Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.
      I am trying to update the database field when the checkbox is clicked. I am trying to modified the following solution but.. got stuck on the...
    3. CheckBox in DataGrid .
      How can I judge which checkbox is checked ? for(int i=0;i<dgOrderList.Items.Count;i++) { CheckBox...
    4. Added CheckBox to a DataGrid Doesn't work with DataGrid.Enabled=False
      I have created a Template Column and Added to a Datagrid, which contains a checkBox. The column is not Binded to any column of the Dataset, but is...
    5. DataGrid&CheckBox
      I have a problem with handle an exception on DataGrid - TemplateColumn I have a datagrid with many columns. One of them is itemID. Second is...
  3. #2

    Default how to add checkbox to a datagrid?

    You can use

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

    HTH

    Elton Wang
    [email]elton_wang@hotmail.com[/email]


    >-----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: :[url]http://das.aspweb.cz/WebForm1.aspx[/url]
    >
    >
    >
    ><%@ 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.DataGridCommandEventHan dler
    (this.DataGrid1_ItemCommand);
    > this.Load += new
    >System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    > private void DataGrid1_ItemCommand(object
    source,
    >System.Web.UI.WebControls.DataGridCommandEventArg s e)
    > {
    > Response.Write(e.Item.Cells
    [0].Text + ":
    >checked-");
    >
    >Response.Write(((CheckBox)e.Item.FindControl
    ("check")).Checked.ToString());
    > }
    > }
    >}
    >
    >.
    >
    Elton W Guest

  4. #3

    Default Re: how to add checkbox to a datagrid?

    Check out this site.
    [url]http://ww.c-sharpcorner.com/code/2003/june/DatagridAndCheckBoxes.asp[/url]
    sandy via DotNetMonster.com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139