Chetan Rajput

Chetan Rajput

  • NA
  • 9
  • 1.4k

Set default value in combo box in grid view take data in c#

Oct 10 2018 3:07 PM
namespace Combobox_control
{
public partial class Form1 : Form
{
static string constr = "server=techs;database=emp_chetan; user id=sa; password=!nudaspc08;";
string combobox;
SqlConnection con = new SqlConnection(constr);
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = Loaddata();
fillcolumn();
//compair();
}
//fill textbox columns
private DataTable Loaddata()
{
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommand cmd;
string sql= "select * from Gridview_windows ";
cmd = new SqlCommand(sql, con);
adapter.SelectCommand = cmd;
adapter.Fill(ds);
dt = ds.Tables[0];
return dt;
}
// fill combobox columns
private void fillcolumn()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
//combo.HeaderText = "Country";
combo.Name = "Selected Country";
ArrayList row = new ArrayList();
//add items for combobox from table
foreach(DataRow dr in dt.Rows)
{
row.Add(dr["Country"].ToString());
}
//add to combo
combo.Items.AddRange(row.ToArray());////
dataGridView1.Columns.Add(combo);
}
private void compair()
{
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommand cmd;
//Int32 i DataGridViewSelectedCellCollection select = new DataGridViewSelectedCellCollection(cmd);
//Int32 selectedColumnCount = dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Selected);
// Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
string sql = "select country from Gridview_windows where country='" + combobox + "' ";
cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
//adapter.SelectCommand = cmd;
//adapter.Fill(ds);
while(dr.Read())
{
string country = (string)dr["country"];
}
con.Close();
}
}
}

Answers (2)