Click here to Skip to main content
14,384,585 members
Rate this:
Please Sign up or sign in to vote.
hello,

I Have a Table in Database with some columns and when I bind that table to Datagridview using dataset I want a Combobox and CheckBox to some Columns of Dataset and Others are Text/String.

Also need to fire Events Of That Components.

How can I achieve it?

What I have tried:

I tried below code:
DataGridViewCheckBoxColumn colType = new DataGridViewCheckBoxColumn();
BindingSource wizardBindingSource = new BindingSource();
wizardBindingSource.DataSource = ds;
colType.HeaderText = "PMA";
dgvArtWork.Columns.Insert(8, colType);

but the problem is it inserts a new column but I already have that column in My Dataset i.e. in ds(Which I have used)
Posted
Updated 25-Jun-16 6:10am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Here is an example that uses a BindingList, which can be adapted for database usage.
The BindingList uses this class:
public class dgvClass1 : ICloneable
{
    /// <summary>
    /// priority is not a propery, so it is not visible in datagrid by default.
    /// </summary>
    public string priority;

    /// <summary>
    /// date is not a propery, so it is not visible in datagrid by default.
    /// </summary>
    public DateTime date;

    public int Number { get; set; }
    public string Name { get; set; }

    public dgvClass1()
    {
        this.date = DateTime.Now;
        this.priority = "Medium";
    }

    /// <summary>
    /// https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.100).aspx
    /// </summary>
    public object Clone()
    {
        return (dgvClass1)this.MemberwiseClone();
    }
}


The Winform code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace TestForm1
{
    /// <summary>
    /// Test data grid with BindingList.
    /// Replace column with ComboBox.
    /// Replace date column with class CalendarColumn.
    /// </summary>
    public partial class Form1 : Form
    {
        public BindingList<dgvClass1> bindingList;

        public Form1()
        {
            InitializeComponent();

            // Allow user to resize column widths.
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            this.dataGridView1.AllowUserToAddRows = false;

            this.dataGridView1.DataBindingComplete += (s, ev) => Debug.WriteLine("BindingComplete");
            //this.dataGridView1.CurrentCellDirtyStateChanged += new DataGridViewRowEventHandler(this.DataGridChanged);
            this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
            this.dataGridView1.RowValidated += new DataGridViewCellEventHandler(this.RowValidatedEvent);
        }

        /// <summary>
        /// Fill the BindingList and set the dataGridView1.DataSource.
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            bindingList = new BindingList<dgvClass1>();
            bindingList.AllowNew = true;
            bindingList.AllowRemove = true;

            if (this.dataGridView1.DataSource != null)
            {
                this.dataGridView1.DataSource = null;
                this.dataGridView1.Columns.Remove("Priority");
            }

            this.AddTestData();
            this.AddComboBox();
            this.AddCalendarColumn();

            bindingList.AddingNew += (s, ev) => Debug.WriteLine("AddingNew");
            bindingList.ListChanged += (s, ev) => Debug.WriteLine("ListChanged");
        }

        /// <summary>
        /// Add test data.
        /// </summary>
        private void AddTestData()
        {
            // Add row with test data.
            var item = new dgvClass1();
            item.Number = 1;
            item.Name = "Test data1";
            item.priority = "Low";          // Not visible field will be used in ComboBox later.
            bindingList.Add(item);

            // Add row with test data.
            item = new dgvClass1();
            item.Number = 2;
            item.Name = "Test data2";
            item.date = item.date.AddMonths(1);
            bindingList.Add(item);

            // Add row with test data.
            item = new dgvClass1();
            item.Number = 3;
            item.Name = "Test data3";
            item.date = item.date.AddMonths(2);
            bindingList.Add(item);

            var clone = (dgvClass1)item.Clone();
            clone.Number++;
            bindingList.Add(clone);

            clone = (dgvClass1)clone.Clone();
            clone.Number++;
            bindingList.Add(clone);

            this.dataGridView1.DataSource = bindingList;
            this.dataGridView1.Columns[0].Frozen = true;
        }

        /// <summary>
        /// Add ComboBox column at position 2.
        /// </summary>
        private void AddComboBox()
        {
            DataGridViewComboBoxColumn dgvCombo = new DataGridViewComboBoxColumn();
            dgvCombo.Name = "Priority";
            dgvCombo.Width = 100;
            dgvCombo.DataSource = new string[] { "Low", "Medium", "High" };
            dgvCombo.DisplayIndex = 2;
            this.dataGridView1.Columns.Add(dgvCombo);

            for (int rowNr = 0; rowNr < bindingList.Count; rowNr++)
            {
                var row = this.dataGridView1.Rows[rowNr];
                DataGridViewComboBoxCell dgvComboCell = (DataGridViewComboBoxCell)row.Cells["Priority"];
                dgvComboCell.Value = bindingList[row.Index].priority;
            }
        }

        /// <summary>
        /// Uses class CalendarColumn.
        /// https://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.100).aspx
        /// </summary>
        private void AddCalendarColumn()
        {
            CalendarColumn col = new CalendarColumn();
            col.Name = "Datum";
            col.Width = 100;
            this.dataGridView1.Columns.Add(col);
            ////this.dataGridView1.Columns["Datum"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm";   // "dd/MM/yyyy";

            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                row.Cells["Datum"].Value = bindingList[row.Index].date;
            }
        }

        public void DataGridChanged(object sender, DataGridViewRowEventArgs e)
        {
            Debug.Print("CollectionChanged");
        }

        private void RowValidatedEvent(object sender, DataGridViewCellEventArgs e)
        {
            Debug.Print("RowValidatedEvent");
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Priority")
            {
                string oldPriority = this.bindingList[e.RowIndex].priority;
                string newPriority = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                this.bindingList[e.RowIndex].priority = newPriority;
                //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                Debug.Print("Priority changed from: " + oldPriority + " to: " + newPriority);
            }
            else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Datum")
            {
                ////DateTime oldDate = this.bindingList[e.RowIndex].date;
                DateTime newDate;
                DateTime.TryParse(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out newDate);
                this.bindingList[e.RowIndex].date = newDate;
            }
        }

        /// <summary>
        /// Show changes in bindingList.
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            string str = string.Empty;

            foreach (var item in this.bindingList)
            {
                str += item.Name + ", " + item.priority + ", " + item.date + "\n";
            }

            MessageBox.Show(str);
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                this.bindingList.RemoveAt(this.dataGridView1.CurrentRow.Index);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            var item = new dgvClass1();
            bindingList.Add(item);
            this.dataGridView1.Rows[this.bindingList.Count - 1].Cells["Priority"].Value = item.priority;    // "Medium"
            this.dataGridView1.Rows[this.bindingList.Count - 1].Cells["Datum"].Value = item.date; 
        }
    }
}
   
Comments
bhavikadb 30-Jun-16 7:26am
   
Thanks you RickZeeland for the solution. I got a solution earlier but now I'm stuck between event handling for a comboboxcolumn as I have two comboboxcolumn in a grid.

What I Have tried is:

private void dgvArtWork_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgvArtWork.CurrentCell.ColumnIndex == 14 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += adColumnComboSelectionChanged;

}
else if (dgvArtWork.CurrentCell.ColumnIndex == 16 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += logoColumnComboSelectionChanged;

}
}

private void adColumnComboSelectionChanged(object sender, EventArgs e)
{
var currentcell = dgvArtWork.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
DataGridViewComboBoxCell cel = (DataGridViewComboBoxCell)dgvArtWork.Rows[currentcell.Y].Cells[16];
DataGridViewComboBoxCell celAD = (DataGridViewComboBoxCell)dgvArtWork.Rows[currentcell.Y].Cells[14];
DataTable dsMaster = new DataTable();

string item = dgvArtWork.Rows[currentcell.Y].Cells[5].Value.ToString();
string size = dgvArtWork.Rows[currentcell.Y].Cells[6].Value.ToString();
string ad = sendingCB.EditingControlFormattedValue.ToString();
//string ad = dgvArtWork.Rows[currentcell.Y].Cells[14].FormattedValue.ToString();

cn.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select Logo from ArtWorkMaster where CustCode='" + txtCustCode.Text + "' and AD='" + ad + "' and item='" + item + "' and size='" + size + "'", cn);
adp.Fill(dsMaster);
adp.Dispose();
cn.Close();

for (int i = 0; i < dsMaster.Rows.Count; i++)
{
cel.Items.Add(dsMaster.Rows[i].ItemArray[0]);
}
}

private void logoColumnComboSelectionChanged(object sender, EventArgs e)
{
var currentcell = dgvArtWork.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
//DataGridViewComboBoxCell cel = (DataGridViewComboBoxCell)dgvArtWork.Rows[currentcell.Y].Cells[18];
//DataGridViewComboBoxCell celAD = (DataGridViewComboBoxCell)dgvArtWork.Rows[currentcell.Y].Cells[14];
DataTable dsMaster = new DataTable();

string item = dgvArtWork.Rows[currentcell.Y].Cells[5].Value.ToString();
string size = dgvArtWork.Rows[currentcell.Y].Cells[6].Value.ToString();
string ad = dgvArtWork.Rows[currentcell.Y].Cells[14].Value.ToString();
string logo = sendingCB.EditingControlFormattedValue.ToString();

cn.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select Logo from ArtWorkMaster where CustCode='" + txtCustCode.Text + "' and AD='" + ad + "' and item='" + item + "' and size='" + size + "'", cn);
adp.Fill(dsMaster);
adp.Dispose();
cn.Close();
}

The problem is whenever I select a item from any combobox it fires both the event.

Please can you help we with it
RickZeeland 30-Jun-16 13:19pm
   
Can't you use something like this:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Priority")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100