Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I have datagrid view and in it datagridcombobox column. I want to bind data to that datagrid combo box column
Posted

Soft009 wrote:
I have datagrid view and in it datagridcombobox column.


i really didnt get this part

Soft009 wrote:
I want to bind data to that datagrid combo box column


but i did understand a bit here, u mean u wana bind data to the combobox right well then her's something for ya

explanation: what i have in this code is a combobox and gridview if i select a particular field from my combobox then all the data related to that particular field will be fetched up and will be shown in the datagrid

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //use this namespace for data connectivity with sql

namespace Record_Holder
{
    public partial class InquiryDetails : Form
    {
        public InquiryDetails()
        {
            InitializeComponent();
        }
//on page load data will be filled in the combobox
        private void InquiryDetails_Load(object sender, EventArgs e)
        {

            dataGridView1.Visible = false;
//create the object of sqlconnection class 
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            conn.Open();
            string command = "select distinct time_of_inquiry from Inquiry_Table";
            SqlCommand cmd = new SqlCommand(command, conn);
            
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
//adding data to combobox
                comboBox1.Items.Add(dr[0].ToString());
            }
            conn.Close();
//close the connection 
        }
//you have a selected index change event i have wrote the code in this event so that whenever any value is selected based upon that value i can filter up my records

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x=0;
            SqlConnection connect = new SqlConnection();
            connect.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            connect.Open();
            string mycommand = "select * from Inquiry_Table where time_of_inquiry='" + comboBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(mycommand, connect);
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataTable dt;
            adp.Fill(ds);
            dt = ds.Tables[0];
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                dataGridView1.Visible = true;
                dataGridView1.DataSource = dt;
                x = x + 1;              
            }
//also i have shown a messagebox to display total record found
            MessageBox.Show("Total Inquiry found " + x.ToString(),"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            connect.Close();

        }

        
    }
}


Do rate my answer once you find it useful

Thanks & Regards
Radix :rose:
 
Share this answer
 
v2
Comments
Soft009 21-May-10 4:59am    
this is help full and thankx......but realy I want to bind data to datagridcomboboxcolumn..................combo box inside the datagrid...is it possible......please help me..
thank you
You can do that programatically and this[^] page on MSDN shows how to do that.

Or you can do it in the designer. Select your DataGridView and then the Smart tag (little arrow at top right) from the smart menu select 'Edit Columns'. When the dialog opens select your DataGridViewComboBoxColumn and you will find the DataSource, DisplayMemeber etc. in the property grid.
 
Share this answer
 
Comments
Soft009 4-Jun-10 1:01am    
thnx a lot.........................

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900