DataGridViewComboBoxColumn Selectionchanged

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 Win
{
public partial class Form1 : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter ada;
DataSet ds;
public string a;
DataGridViewComboBoxColumn co;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection(“Data Source=.;Initial Catalog=LMDB;Integrated Security=True”);
con.Open();
cmd = new SqlCommand(“select code from SampleCustomer”, con);
ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds, “SampleCustomer”);
dataGridView1.DataSource = ds.Tables[“SampleCustomer”].DefaultView;
dgvcombocolumn();

}

public void dgvcombocolumn()
{
co = new DataGridViewComboBoxColumn();

// co.DataSource = ds.Tables[“SampleCustomer”];
// co.ValueMember = “code”;
// co.DisplayMember = “code”;
co.Items.Add(“hi”);
co.Items.Add(“hi1”);
co.Items.Add(“hi2”);
co.HeaderText = “codee”;
// this.co. += new System.EventHandler(this.TextBox_TextChanged);

dataGridView1.Columns.Add(co);
}

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//if (dataGridView1.Columns[e.ColumnIndex].Name == “code”)
//{
//    int i = dataGridView1.SelectedCells[0].RowIndex;
//    ada = new SqlDataAdapter(“select * from Pr_Components”, con);
//    ds = new DataSet();
//    ada.Fill(ds, “code”);
//    a = ds.Tables[0].Rows[i][0].ToString();
//    MessageBox.Show(a);
//}
}

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);

// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}

}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string a= ((ComboBox)sender).SelectedItem.ToString();
if (a == “hi”)
{
dataGridView1.Columns.Remove(co);
dgvcombocolumn();
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[“code”].Value = 15;
//co.Items.Remove(a);
//co.Items.Insert(0, a);
//co.HeaderCell.Value = a;

}
if (a == “hi1”)
{

dataGridView1.Columns.Remove(co);
dgvcombocolumn();
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[“code”].Value = 20;
// co.HeaderCell.Value = a;
//co.Items.Remove(a);
//co.Items.Insert(0, a);
}
if (a == “hi2”)
{

dataGridView1.Columns.Remove(co);
dgvcombocolumn();
co.HeaderCell.Value = a;
MessageBox.Show(“XxXxXx”);
}

}
catch (Exception Ex)
{
}
//  ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
}

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

}

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: