ARTICLE

Revise-Generating ComboBox in a DataGrid Column

Posted by Paul Coldrey Articles | Windows Controls C# June 10, 2004
This article and attached source code shows how to add a ComboBox column to a DataGrid control.
Reader Level:
Download Files:
 

Notes

A ColumnStyle to let you have a combobox column in a .NET DataGrid. The URL is the link where I lifted most of the code from. I fixed a couple of things like showing the DisplayMember not the value member when the item doesn't have focus + I made it so if you select something from the combo box then it makes that row of the DataGrid commit even if no other changes are made.


Associated URL
http://www.c-sharpcorner.com/winforms/ComboBoxInDataGridSKJ.asp

Login to add your contents and source code to this article
post comment
     

I have some question ,i don't know how to do it .
i have three table in my database access :
-attendees
-events
-registration
there is a combox what is represented for companyName in attendees table
i had a form what is written by c# when i select companyName in combox ,it will display some information that relevant to this company name, in this part i did it,but  i have also a datagridview  ,and i need when i click on combox it not only display as i said before but also display some information of event of this company ,so i try to do many way but in this part i can't .the Error said that "Data type mismatch in criteria expression." this is  my code:
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.OleDb;

namespace project2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\dquang\\Desktop\\project2.mdb");
            con.Open();
            OleDbDataAdapter adap = new OleDbDataAdapter("select * from Attendees", con);
            DataSet ds=new DataSet();
             adap.Fill(ds);
             comboBox1.DataSource = ds.Tables[0];
             comboBox1.DisplayMember = "CompanyName";
             comboBox1.ValueMember = "CompanyName";


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string company = comboBox1.SelectedValue.ToString();
            OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\dquang\\Desktop\\project2.mdb");
            con1.Open();
            OleDbDataAdapter adap1 = new OleDbDataAdapter("select * from Attendees where CompanyName='" + company + "'", con1);
            DataSet ds1 = new DataSet();
            //adap1.Fill(ds1);
            adap1.Fill(ds1, "Attendees");
            DataTable dt = new DataTable();
            dt = ds1.Tables["Attendees"];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                lattendeeID.Text = row["AttendeeID"].ToString();
                txtAddress.Text = row["Address"].ToString();
                txtCity.Text = row["City"].ToString();
                txtStateProvince.Text = row["StateOrProvince"].ToString();
                txtPostalcode.Text = row["PostalCode"].ToString();
                txtCountry.Text = row["Country"].ToString();
                txtFirstName.Text = row["AttendeeFirstName"].ToString();
                txtLastName.Text = row["AttendeeLastName"].ToString();
                txtTitle.Text = row["title"].ToString();
                txtPhone.Text = row["PhoneNumber"].ToString();
                txtFax.Text = row["FaxNumber"].ToString();
                txtNotes.Text = row["Notes"].ToString();
            }

            string sql = "select EventName,StartDate,SalesTaxRate from Events,Registration where AttendeeID='" + lattendeeID.Text + "'";
            OleDbConnection con2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\dquang\\Desktop\\project2.mdb");
            con2.Open();
            OleDbDataAdapter adap3 = new OleDbDataAdapter(sql, con2);
            DataSet ds3 = new DataSet();
            adap3.Fill(ds3);=======error (i don't know how to handle,please help me)
            gridviewEvent.DataSource = ds3.Tables[0];

Posted by duyquang le Mar 14, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.