how to put the combobox in that gridview.when we select the combobox then display the another combobox values in that datagridview. data retrive from the database.


i want the datagridview colums is:

Sno ProductsName Items Quantity Total.


ProductName is one combobox and Iems is another combobox.when we select the productname then display the particular product name Itmes.

This grid is entirely set up in code. You can add a combo box to a gridview as 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;
using System.Data.SqlClient;

namespace daniweb
{
  public partial class frmGridView3 : Form
  {
    private DataTable _dt;
    private DataGridViewTextBoxColumn _colInvNumber;
    private DataGridViewComboBoxColumn _colLocNumber;

    public frmGridView3()
    {
      InitializeComponent();
    }

    private void SetupGrid()
    {
      _colInvNumber = new DataGridViewTextBoxColumn();
      _colInvNumber.DataPropertyName = "InvNumber";
      _colInvNumber.HeaderText = "Invoice #";
      _colInvNumber.Name = "colInvNumber";

      _colLocNumber = new DataGridViewComboBoxColumn();
      _colLocNumber.DataPropertyName = "LocNumber";
      _colLocNumber.HeaderText = "Location";
      _colLocNumber.Name = "colLocNumber";
      _colLocNumber.DataSource = GetLocationNumbers();
      _colLocNumber.DataPropertyName = "LocNumber";
      _colLocNumber.DisplayMember = "LocNumber";
      

      this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] { _colInvNumber, _colLocNumber });
    }

    //This is where i populate the combobox from the DB
    private static DataTable GetLocationNumbers()
    {
      const string query = "Select Distinct(LocNumber) As LocNumber From Invoice";
      return RunQuery(query);
    }

    private void PopulateGrid()
    {
      const string query = "Select Top 30 InvNumber, LocNumber From Invoice";
      if (_dt != null)
      {
        dataGridView1.DataSource = null;
        _dt.Dispose();
        _dt = null;
      }
      _dt = RunQuery(query);
      dataGridView1.DataSource = _dt;
    }

    private static DataTable RunQuery(string query)
    {
      const string connStr = @"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
      DataTable result = new DataTable();
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          using (SqlDataReader dr = cmd.ExecuteReader())
          {
            result.Load(dr);
          }
        }
      }
      return result;
    }

    private void frmGridView3_Load(object sender, EventArgs e)
    {
      SetupGrid();
      PopulateGrid();
    }

    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
      System.Diagnostics.Debugger.Break();
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.