You must Sign In to post a response.
  • Category: .NET

    How to set check box in datagridview header text?

    Hi Friends,

    Can you please help me out for this..

    I have a datagridview in which I want to set first column header text as Check box and column type as datagridviewcheckboxcolumn.
    In my project scenario like
    If I check the header text checkbox , rest of all data check boxes(i.e., the following checkboxes under header check box column) in DGV will also in check mode, when I uncheck on any one of check box row then automatically it reverts all checkbox columns and header check box become uncheck

    If I Check the checkbox column in any row, total row will be selected

    If I uncheck on the header text checkbox , rest of all data check boxes in DGV will also in Uncheck mode

    If I check the column header and click on delete button then all the checked column rows in DGV will be deleted

    Thanks in Advance,
    Srujana
  • #723945
    please below link are check :

    http://www.codeproject.com/Questions/271844/how-to-check-all-checkboxes-in-gridview-when-check

    http://www.aspsnippets.com/Articles/Implement-check-all-checkbox-functionality-in-ASPNet-GridView-control-using-JavaScript.aspx

    add delete button :

    http://csharpdotnetfreak.blogspot.com/2009/04/delete-multiple-rows-gridview-checkbox.html

    I Hope To Use Full..
    And Choice The Best Answers

    php-wordpress Developer
    you can any problem to php wordpress to contact me
    jivanibhai7306@gmail.com
    Thanks!!...

  • #723949
    Thanq Jivani,

    What I have posted the question that is C# Windows Application
    Sorry I forgot to mention there

    Can you have any more links regarding Windows Application for the same question..

    Kindly post...

    Regards,
    Srujana

  • #723955
    Hai srujana,

    Use the below sample code.

    Hope this code will work



    < script type="text/Javascript" >

    function Check(parentChk,container)
    {
    var elements = document.getElementsByTagName("INPUT");
    for(i=0; i<elements.length;i++)
    {
    if(parentChk.checked == true)
    {
    if( IsCheckBox(elements[i]) && IsMatch(elements[i].id,container))
    {
    elements[i].checked = true;
    }
    }
    else
    {
    if( IsCheckBox(elements[i]) && IsMatch(elements[i].id,container))
    {
    elements[i].checked = false;
    }
    }
    }
    }
    < script>


    Source code:

    Call that Javascript function in header control checkbox.



    <asp:GridView ID="GV" runat="server" ShowFooter="false" Width="100%"
    EnableModelValidation="True" ForeColor="#333333"
    GridLines="Vertical" AutoGenerateColumns="false" >

    <Columns>
    <asp:TemplateField >
    <HeaderTemplate>
    <input id="HchkSelect" type="checkbox" onclick="javascript:Check(this,'GV');" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkSelect" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="center" />
    </asp:TemplateField>
    </Columns>
    </asp:GridView>


    Hope this information is helpful to you..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #723984
    use this mark up to get check box on header
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckUncheckAll" />
    </HeaderTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <HeaderTemplate>
    Name
    </HeaderTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <HeaderTemplate>
    Age
    </HeaderTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <HeaderTemplate>
    Address
    </HeaderTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>

  • #724017
    HI,

    Refer my detailed code snippet as per your requirement


    using System.Text;
    using System.Windows.Forms;

    namespace CShChkColumn
    {
    public partial class Form1 : Form
    {
    CheckBox chkbox=new CheckBox();
    Program p=new Program();
    DataTable dt = new DataTable();

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    loadGrid();
    }

    void loadGrid()
    {
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.Columns.Clear();
    DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
    colCB.Name = "chkcol";
    colCB.HeaderText = "";
    colCB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    dataGridView1.Columns.Add(colCB);
    Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
    chkbox.Size = new Size(18, 18);
    rect.Offset(40, 2);
    chkbox.Location = rect.Location;
    chkbox.CheckedChanged += chkBoxChange;
    this.dataGridView1.Controls.Add(chkbox);
    DataRow dr = default(DataRow);
    dt.Columns.Add("eno");
    dt.Columns.Add("empname");
    dt.Columns.Add("sal");
    dr = dt.NewRow();
    dr["eno"] = 101;
    dr["empname"] = "test1";
    dr["sal"] = 9000;
    dt.Rows.Add(dr);
    dr = dt.NewRow();

    dr["eno"] = 102;
    dr["empname"] = "test2";
    dr["sal"] = 15000;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["eno"] = 103;
    dr["empname"] = "test3";
    dr["sal"] = 20000;
    dt.Rows.Add(dr);

    dataGridView1.DataSource = dt;
    }

    private void chkBoxChange(object sender, EventArgs e)
    {
    for (int k = 0; k <= dataGridView1.RowCount - 1; k++)
    {
    this.dataGridView1[0, k].Value = this.chkbox.Checked;
    }
    this.dataGridView1.EndEdit();
    }
    }
    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #733782
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    System.Data.DataTable dtblSkill = new System.Data.DataTable();
    public Form1()
    {
    InitializeComponent();
    dtblSkill.Columns.Add("SkillCode", typeof(string));
    dtblSkill.Columns.Add("SkillName", typeof(string));
    AddGridViewSkillColumns();
    }
    private void AddGridViewSkillColumns()
    {
    try
    {

    DataGridViewTextBoxColumn dgvtxtSkillCode = new DataGridViewTextBoxColumn();
    dgvtxtSkillCode.Name = "SkillCode";
    dgvtxtSkillCode.DataPropertyName = "SkillCode";
    dgvtxtSkillCode.HeaderText = "Skill Code";
    dgvtxtSkillCode.Width = 120;


    DataGridViewTextBoxColumn dgvtxtSkillName = new DataGridViewTextBoxColumn();
    dgvtxtSkillName.Name = "SkillName";
    dgvtxtSkillName.HeaderText = "Skill Name";
    dgvtxtSkillName.DataPropertyName = "SkillName";
    dgvtxtSkillName.Visible = true;
    dgvtxtSkillName.Width = 160;


    dataGridView1.Columns.Add(dgvtxtSkillCode);
    dataGridView1.Columns.Add(dgvtxtSkillName);

    dataGridView1.DataSource = dtblSkill;
    }
    catch (Exception ex)
    {
    MessageBox.Show("Error Details" + ex.Message);
    }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Microsoft.Office.Interop.Excel.Application wapp;
    Microsoft.Office.Interop.Excel.Worksheet wsheet;
    Microsoft.Office.Interop.Excel.Workbook wbook;
    wapp = new Microsoft.Office.Interop.Excel.Application();
    wapp.Visible = false;
    wbook = wapp.Workbooks.Add(true);
    wsheet = (Worksheet)wbook.ActiveSheet;
    try
    {

    for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
    {
    wsheet.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;

    }

    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
    {
    DataGridViewRow dgvrow = this.dataGridView1.Rows[i];
    for (int j = 0; j < dgvrow.Cells.Count; j++)
    {
    DataGridViewCell cellvalue = dgvrow.Cells[j];
    try
    {
    wsheet.Cells[i + 2, j + 1] = (cellvalue.Value == null) ? "" : cellvalue.Value.ToString();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    }
    wapp.Visible = true;
    }
    catch (Exception ex)
    {
    MessageBox.Show("Error Details" + ex.Message);
    }
    }
    }
    }



    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.