Link to home
Start Free TrialLog in
Avatar of deb_holmes
deb_holmes

asked on

How to populate datasource of EditItemTemplate (within a datagrid) from static list

I've found a number of articles, particularly this one: http://aspnet.4guysfromrolla.com/articles/080702-1.2.aspx
on how to populate a drop down list within a datagrid from a database.  However my dropdown list only has three choices and I don't want to have to hit a DB to fetch it.  I can't find any instructions on how to add items manually to a dataset, not any info to determine if some alternate datstructure, like an array, can be used as a datasouce.
Avatar of Muhammad Kashif
Muhammad Kashif
Flag of Pakistan image

Create a datarow and add values to it and the add that datarow to the dataset's table.
Use following code

DataSet objDS; 
DataRow oDataRow; 
    
oDataRow = objDS.Tables(0).NewRow; 
    
oDataRow(0) = "First Column value"; 
oDataRow(1) = "Second Column Value"; 
objDS.Tables(0).Rows.Add(oDataRow); 

Open in new window

Avatar of deb_holmes
deb_holmes

ASKER

Looks promising thank you.  I am actualy about to go to sleep so I will accept your answer as soon as I verify it in the morning.  Thanks again
You need to make sure that the Dataset you are adding your values to is not just any dataset but the dataset that you are dealing with.  If your DataSet was named MYDataSet, your code in C# would look like this.

DataRow Row = MyDataSet.Tables["YourTableName"].NewRow;
Row[0] = "value";
Row[1] = "value";
MyDataSet.Tables["YourTableName"].Rows.Add(Row);
The first doesn't work and I don't seem to be able to understand how the second pertains to this situation, where it's intended to populate and return a  brand new dataset.
I have:
protected DataSet GetPriceValues()
        {
            DataSet ds = new DataSet();
            DataRow dr;
            dr = ds.Tables[0].NewRow;
            dr[0] = "Gem of the Day";
            dr[1] = "Gem of the Day";
            ds.Tables[0].Rows.Add(dr);

            return ds;
}

where this function is supposed to return the values in the dropdown list associated with this portion of the datagrid definition:

<EditItemTemplate>
                    <asp:DropDownList runat="server" id="lstValueTypes"
                            DataValueField="value"
                            DataTextField="value"
                            DataSource="<%# GetPriceValues() %>" />
                   </EditItemTemplate>    

and I'm getting the following error:

Error      4      Cannot convert method group 'NewRow' to non-delegate type 'System.Data.DataRow'. Did you intend to invoke the method?      

I somewhat randomly tried changing the syntax on the offending line to:
dr = new DataRow(ds.Tables[0]);
and now the error is:
Error      4      'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level      
ASKER CERTIFIED SOLUTION
Avatar of Chris-Chambers
Chris-Chambers
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!
add the desired "choices" into the dropdownlist in the design time itself ..

is the datagrid's column that contains the dropdownlist a templatecolumn? if not, convert it into a template column .. if yes, then in the edittemplate (right click datagrid->edit template and select the column containing the dropdownlist), select the dropdownlist and add the necessary items using IDE .. (right click on the dropdownlist, edit items) or through the aspx code using

<asp:DropDownList ID="Dropdownlist1" Runat="server">
<asp:ListItem>Value1</asp:ListItem>
<asp:ListItem>Value2</asp:ListItem>
....
</asp:DropDownList

Rejo
I just noticed in one of your above comments that you do have the column as a template colum .. so just add the choices to it
<EditItemTemplate>
<asp:DropDownList runat="server" id="lstValueTypes">
<asp:ListItem>Choice1</asp:ListItem>
<asp:ListItem>Choice2</asp:ListItem>
....
</asp:DropDownList>

To set the value of the dropdownlist from the datagrid's row that is been edited .. you will have to use the selectedvalue property of the dropdownlist .. this again can be done by setting it within the aspx code or within server side code
Well that is way to easy.  Thank you!