Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code below works for me, but i have not been able to insert ---Select--- to the for first index with value zero.

What I have tried:

private void LoadGrade()
    {
        using (SQLiteConnection conn = new SQLiteConnection(connstring))
        {

            try
            {
                string query = "select GradeCode, GradeName from Grade";
                SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn);
                conn.Open();
                DataSet ds = new DataSet();
                da.Fill(ds, "OrgGrade");
                cboGrade.Items.Insert(0,"---Select---);
                cboGrade.DisplayMember = "GradeName";
                cboGrade.ValueMember = "GradeCode";
                cboGrade.DataSource = ds.Tables["OrgGrade"];

            }
            catch (Exception ex)
            {

                MessageBox.Show("Error occured loading grade!");
            }
        }
    } 




but it is not working for me. Also i want to able to retrieve and display the displaymember and displayvalue on the combo box while retrieving from database without items not being repeated
Posted
Updated 14-Sep-17 20:41pm

try

DataTable dt = new DataTable();
           dt.Columns.Add("GradeName");
           dt.Columns.Add("GradeCode");
           dt.Rows.Add("aaa", "11");
           var row = dt.NewRow();
           row["GradeName"] = "Select";
           row["GradeCode"] = "0";
           dt.Rows.InsertAt(row, 0);
           cboGrade.DisplayMember = "GradeName";
           cboGrade.ValueMember = "GradeCode";
           cboGrade.DataSource = dt;
 
Share this answer
 
Comments
Uwakpeter 15-Sep-17 0:43am    
I don't want to hardcord list items: dt.Rows.Add("aaa", "11"); i am getting list items from database
Karthik_Mahalingam 15-Sep-17 0:48am    
It's just for an example
You shall use your dataset instead
Karthik_Mahalingam 15-Sep-17 0:49am    
var row = ds.Tables["OrgGrade"].NewRow();
row["GradeName"] = "Select";
row["GradeCode"] = "0";
ds.Tables["OrgGrade"].Rows.InsertAt(row, 0);
cboGrade.DisplayMember = "GradeName";
cboGrade.ValueMember = "GradeCode";
cboGrade.DataSource = ds.Tables["OrgGrade"];
Uwakpeter 15-Sep-17 1:08am    
Yeah, that works thanks, i also want to be able to insert retrieved displaymember and valuemember into the bounded list without duplication that particular list item, say, i am searching for a employee details, and the employee has a grade of senior staff, senior staff is already bounded to list say on index 4, i want to be able to pick it and display, i have tried cboGrade.Items.Insert(0,retrievedValue), this seems to work, but it is duplicating the retrieved item each time the search button is clicked.
Karthik_Mahalingam 15-Sep-17 1:13am    
find that particular item and remove it using "RemoveAt(index);" and the insert the new item
You can't work directly on the ComboBox as you have bound it to a DataSet. Instead, you need to work with where the data is located using DataRowCollection.InsertAt Method (DataRow, Int32) (System.Data)[^]

So example could be (untested):
C#
var insertRow = ds.Details.NewDetailsRow();

insertRow.Id = "0";
insertRow.Description = "---Select---";

ds.Details.Rows.InsertAt(insertRow, 0);
 
Share this answer
 
Comments
Uwakpeter 15-Sep-17 0:10am    
Where is the Details from?
Graeme_Grant 15-Sep-17 0:19am    
Just off the top of my head, hence "(untested)". Have a look at the link provided.
private void LoadGrade()
    {
        using (SQLiteConnection conn = new SQLiteConnection(connstring))
        {
 
            try
            {
                string query = "select GradeCode, GradeName from Grade";
                SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn);
                conn.Open();
                DataSet ds = new DataSet();
                da.Fill(ds, "OrgGrade");
                
                cboGrade.DisplayMember = "GradeName";
                cboGrade.ValueMember = "GradeCode";
                cboGrade.DataSource = ds.Tables["OrgGrade"];
                cboGrade.Items.Insert(0,"---Select---);
 
            }
            catch (Exception ex)
            {
 
                MessageBox.Show("Error occured loading grade!");
            }
        }
    } 
 
Share this answer
 
v2

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