Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my function is :--

C#
public static int varBool;
public static void fillCmbo(ComboBox cmb, string str)
{
    DataSet ds = new DataSet();
    ds = Connector.ExecuteDataset(mainCon.cn, CommandType.Text, str);
    varBool = 0;
    cmb.DataSource = ds.Tables[0];
    cmb.ValueMember = ds.Tables[0].Columns[0].ColumnName;
    cmb.DisplayMember = ds.Tables[0].Columns[1].ColumnName;
    cmb.SelectedIndex =-1;
    varBool = 1;
}


This function work properly but DataBind from 0 index of ComboBox and I try to insert a "<Select Item >" Word in 0 Index after DataBind.

Please help.

Thanks in advance
Posted
Updated 26-Jan-11 7:42am
v2

You haven't shown your SELECT statement so I cannot use your column names and I do not know how many columns you are retrieving, so I will offer a couple of alternatives.

Change your SELECT to something like:

SQL
SELECT "Select", "<Select Item>"
UNION
SELECT valueMemberColumn, displayMemberColumn FROM yourDB


Or
After you have filled the DataSet but before you bind it to the ComboBox, insert a new row into the table with your <Select Item> in the appropriate column.
 
Share this answer
 
v4
You should try the following code,

C#
public static int varBool;
      public static void fillCmbo(ComboBox cmb, string str)
      {
          DataSet ds = new DataSet();
          ds = Connector.ExecuteDataset(mainCon.cn, CommandType.Text, str);
          varBool = 0;
                DataRow dr = ds.Tables[0].NewRow();
                dr[1] = "-Select-";
                dr[0] = "-1";
                ds.Tables[0].Rows.InsertAt(dr, 0);

                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        
                        cmb.DataSource = ds.Tables[0].DefaultView;
                        cmb.ValueMember = ds.Tables[0].Columns[0].ColumnName;
                        cmb.DisplayMember = ds.Tables[0].Columns[1].ColumnName;
                    }
              }
       
      }


Best Regards,
Theingi Win
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900