26 November 2013

Displaying “ Checkbox “ Field in every row in the Table In the Grid View

In this Discussion,i want to informing about Displaying “ Checkbox “ Field in every row in the Table In  “Grid View “ and Create a table Data without DataBase Creation and Displays in GridView.

->Drag and drop the Gridview and button controls from Toolbox in windows form.
->in the form,write the code in the button click event as like shown below :

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

namespace GridViewWithChechBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
// Create Table Data in GridView without Database creation….

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

// display CheckBox Field in the Gridview….code

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            dataGridView1.Columns.Add(chk);
            chk.HeaderText = "Check Data";
            chk.Name = "chk";
            dataGridView1.Rows[2].Cells[3].Value = true;

        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Result is shown below like..




No comments:

Post a Comment