Results 1 to 3 of 3

Thread: How to add check box in a datagrid in windows application

  1. #1
    Junior Member
    Join Date
    Dec 2007
    Answers
    2

    How to add check box in a datagrid in windows application

    How to add check box in a datagrid in windows application (C# 1.14).


    I have created a windows application which consists of data grid with data set . now i need to include a check box in the same grid...

    The row which has selected check box need to perform some action...

    can some one help me out please....


    thanks in advance.....


  2. #2
    Junior Member
    Join Date
    May 2008
    Answers
    8

    Re: How to add check box in a datagrid in windows application

    Hi,

    You have to use tablestyles while bind the data to grid,
    Create DataGridColumnStyle for each column.
    for first column if u need check box follow this code.

    DataGridTableStyle tblstyle = new DataGridTableStyle();

    DataGridBoolColumn chkboxCol=new DataGridBoolColumn();

    discontinuedCol.MappingName = "chkbox";

    discontinuedCol.HeaderText = "";

    discontinuedCol.Width = 30;

    chkboxCol.AllowNull = false;

    tblstyle .GridColumnStyles.Add(chkboxCol);


  3. #3
    Junior Member
    Join Date
    Feb 2009
    Answers
    1

    Re: How to add check box in a datagrid in windows application

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Collections;
    using System.Windows.Forms;

    namespace datagridexample
    {
    public partial class Form1 : Form
    {
    public Form1(ArrayList items, ArrayList op)
    {
    InitializeComponent();
    this.LlenarDG(this.dataGrid1, items, op);
    }
    private void LlenarDG(DataGrid dg, ArrayList items, ArrayList op)
    {
    //limpiar el dg por si tiene algo
    dg.DataSource = null;
    DataTable dt = new DataTable("Listado");
    DataColumn[] collumns = new DataColumn[op.Count +1];
    collumns[0] = new DataColumn("Item", typeof(string));
    dt.Columns.Add(collumns[0]);
    for (int i = 0; i < op.Count; i++)
    {
    string text = (string)op[i];
    dt.Columns.Add(new DataColumn(text, typeof(bool)));
    }
    DataSet ds = new DataSet();
    ds.Tables.Add(dt);
    dg.SetDataBinding(ds, "Listado");
    AddData(dt,items);
    //ajustar el tamano de las columnas
    this.SizeColumnsToContent(dg,op.Count,dg.Width);
    }
    public void SizeColumnsToContent(DataGrid dataGrid, int CantidadColumnas, int AnchoControl)
    {
    // Create graphics object for measuring widths.
    Graphics Graphics = dataGrid.CreateGraphics();

    // Define new table style.
    DataGridTableStyle tableStyle = new DataGridTableStyle();

    try
    {
    DataTable dataTable = ((DataSet)dataGrid.DataSource).Tables[0];
    // Clear any existing table styles.
    dataGrid.TableStyles.Clear();
    // Use mapping name that is defined in the data source.
    tableStyle.MappingName = dataTable.TableName;

    // Now create the column styles within the table style.
    //DataGridTextBoxColumn columnStyle;
    int iWidth = (AnchoControl - 70) / CantidadColumnas;
    // Create the style for the first collumn
    DataColumn dataColumn = dataTable.Columns[0];
    DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
    columnStyle.TextBox.Enabled = true;
    columnStyle.HeaderText = dataColumn.ColumnName;
    columnStyle.MappingName = dataColumn.ColumnName;
    // Set width to header text width.
    iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);
    // Change width, if data width is wider than header text width.
    // Check the width of the data in the first X rows.
    DataRow dataRow;
    for (int iRow = 0; iRow < dataTable.Rows.Count; iRow++)
    {
    dataRow = dataTable.Rows[iRow];

    if ( dataRow[dataColumn.ColumnName] != null)
    {
    int iColWidth = (int)(Graphics.MeasureString(dataRow.
    ItemArray[0].ToString(),
    dataGrid.Font).Width);
    iWidth = (int)System.Math.Max(iWidth, iColWidth);
    }
    }
    columnStyle.Width = iWidth+2;
    // Add the new column style to the table style.
    tableStyle.GridColumnStyles.Add(columnStyle);

    // Create the style for the rest of the collumns
    DataGridBoolColumn columnStyle1 = new DataGridBoolColumn();
    for (int iCurrCol = 1; iCurrCol < dataTable.Columns.Count;
    iCurrCol++)
    {
    dataColumn = dataTable.Columns[iCurrCol];

    columnStyle1 = new DataGridBoolColumn();
    columnStyle1.HeaderText = dataColumn.ColumnName;
    columnStyle1.MappingName = dataColumn.ColumnName;
    //columnStyle1.FalseValue = false;
    //columnStyle1.TrueValue = true;
    // Set width to header text width.
    iWidth = (int)(Graphics.MeasureString(columnStyle1.HeaderText,
    dataGrid.Font).Width);

    // Change width, if data width is wider than header text width.
    // Check the width of the data in the first X rows.
    for (int iRow = 0; iRow < dataTable.Rows.Count; iRow++)
    {
    dataRow = dataTable.Rows[iRow];
    if (null != dataRow[dataColumn.ColumnName])
    {
    int iColWidth = (int)(Graphics.MeasureString(dataRow.
    ItemArray[iCurrCol].ToString(),
    dataGrid.Font).Width);
    iWidth = (int)System.Math.Max(iWidth, iColWidth);
    }
    }
    columnStyle1.Width = iWidth+2;
    // Add the new column style to the table style.
    tableStyle.GridColumnStyles.Add(columnStyle1);
    }
    // Add the new table style to the data grid.
    dataGrid.TableStyles.Add(tableStyle);
    //dataGrid.Width =
    }
    catch (Exception e)
    {
    MessageBox.Show(e.Message);
    }
    finally
    {
    Graphics.Dispose();
    }
    }
    private void AddData(DataTable t, ArrayList i)
    {
    // Add data with for the id field.
    DataRow row;
    foreach (string s in i)
    {
    row = t.NewRow();
    row["Item"] = s;
    t.Rows.Add(row);
    }
    t.AcceptChanges();
    }
    }

    }




  4.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact