none
how to add a row to DataTableCollection RRS feed

  • Question

  • Hi, I have a list of states that is called to a class and displayed as a drop down menu.  It is converted to a list, and I need to add a blank row to the top of the drop down menu.  How would I go about doing this?  Thanks
    Thursday, June 21, 2007 5:26 PM

Answers

  • ok so you are "binding" the datasource to the combo. Like I stated, if you do this then you cannot manually add the blank row. What you need to do is the following

     

     

    if (string.Equals(refTableName, "states", StringComparison.InvariantCultureIgnoreCase))

    {

    DataSet ds = DBUtils.GetDataSet(

    "Select * from " + refTableName + " order by " + OrderByMem + " " + OrderByDir,

    DBConnectorFactory.DB_SCHEMA.DB_THE_DB);

    dropdown1.items.add("");

    foreach ( DataRow row  in ds.Tables[0].Rows)

    {

        dropdown1.items.add(row[StateColumnNameorOrdinal].ToString() );

    }

    }

     

    Basically you are looping through the DataRows in the DataTable[0] and grabbing the value of the column that holds the State value, explicitly casting to type string, and adding it to the combobox.

     

    -paul

    Friday, June 22, 2007 10:02 AM

All replies

  • doing it this way you add the blank line then you add the rest of the items. If you added the blank and then bind, you wipe out the blank.

     

    c# code

     

    dropdown1.items.add("");

    foreach ( State s in ListStates)

    {

    dropdown1.items.add(s.ToString() );

    }

    Friday, June 22, 2007 1:29 AM
  • I am having trouble implementing this.  Here is how the list of states is populated, maybe i did not pass enough information along at first.

     

    if (string.Equals(refTableName, "states", StringComparison.InvariantCultureIgnoreCase))

    {

    DataSet ds = DBUtils.GetDataSet(

    "Select * from " + refTableName + " order by " + OrderByMem + " " + OrderByDir,

    DBConnectorFactory.DB_SCHEMA.DB_THE_DB);

    cb.DataSource = ds.Tables[0];

    }

    Friday, June 22, 2007 7:40 AM
  • ok so you are "binding" the datasource to the combo. Like I stated, if you do this then you cannot manually add the blank row. What you need to do is the following

     

     

    if (string.Equals(refTableName, "states", StringComparison.InvariantCultureIgnoreCase))

    {

    DataSet ds = DBUtils.GetDataSet(

    "Select * from " + refTableName + " order by " + OrderByMem + " " + OrderByDir,

    DBConnectorFactory.DB_SCHEMA.DB_THE_DB);

    dropdown1.items.add("");

    foreach ( DataRow row  in ds.Tables[0].Rows)

    {

        dropdown1.items.add(row[StateColumnNameorOrdinal].ToString() );

    }

    }

     

    Basically you are looping through the DataRows in the DataTable[0] and grabbing the value of the column that holds the State value, explicitly casting to type string, and adding it to the combobox.

     

    -paul

    Friday, June 22, 2007 10:02 AM