RSS

add an item to combobox before bind data

12 Jun

Solution 1:

DataTable dt = new DataTable();

// cboTown.Items.Clear();

if (obj.RowCount > 0)
{
// cboTown.Items.Insert(-1, “—-For All—-“);
dt = obj.DefaultView.Table;
DataRow row = dt.NewRow();
row[“town”] = “—-For All—-“;
row[“surveyformid”] = “0”;
dt.Rows.InsertAt(row, 0);

cboTown.DataSource = dt;
cboTown.DisplayMember = “town”;
cboTown.ValueMember = “surveyformid”;
this.cboTown.SelectedIndex = 0;
}

Solution 2;

DataTable table = new DataTable("myData");
using (SqlConnection conn = new SqlConnection(connString))
{
    using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM MyTable", conn))
         da.Fill(table);
}

DataRow row  = table.NewRow();
row["town"] =  "Please select bellow...";
  row["surveyformid"] = "0";
table.Rows.InsertAt(row, 0); 
 cboTown.DataSource = table;
                cboTown.DisplayMember = "town";
                cboTown.ValueMember = "surveyformid";
                this.cboTown.SelectedIndex = 0;
  this.cboTown.SelectedIndex = 0;

Untitled
Advertisements
 
Leave a comment

Posted by on June 12, 2013 in WinForm

 

Tags: ,

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: