C# -- Datagrid and combobox -- wanting to show current dataset value with combobox for changing value

I am using asp.net where i am wanting to use a datagrid with the column of 'location'.  When the datagrid is loaded I would like it two have the value of the current dataset with respect to 'location' where I can then change the value through a combobox.  The way I have the following 'location' has the values from the location column within the combobox, but it does not default to the current value of the dataset.  I guess what I am trying to do is default the combobox item to the corresponding dataset value per the individual datarow.  I am not sure how to do this though.

When data is changed and all changes are complete I will loop through the datagrid and modify the dataset.  This part I am able to do.

--BIND DATA TO DATAGRDID COMBOBOX THREE
<Columns>
<asp:TemplateColumn HeaderText="location">
<ItemTemplate>
<asp:DropDownList id="Dropdownlist1" runat="server" DataSource="<%# dsSql %>" DataMember="WorkRequest" DataTextField="location" DataValueField="location" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="ITEMNUM">
<ItemTemplate>
<asp:TextBox Font-Bold=True MaxLength="10" Width="80" runat="server" id="ITEMNUM" text='<%# DataBinder.Eval(Container.DataItem, "ITEMNUM") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<Columns>

Thanks!
vandy02Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darth_helgeCommented:
i guess you can do it in the databound event in your datagrid

private void OnItemDataBound(object sender,
        System.Web.UI.WebControls.DataGridItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item ||  e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList d = (DropDownList)e.Item.FindControl("NameOfYourDDL");
//Then you can fetch the value. PS! I dont know which cell your data lies
string ValueToSetInDDL = e.Item.Cells[2].Text;
//Or maybe
string ValueToSetInDDL = (TextBox)e.Item.FindControl("NameOfTXT").Text;

d.SelectedValue = ValueToSetInDDL
}

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vandy02Author Commented:
e.Item.Cells[5]
0
Darth_helgeCommented:
private void OnItemDataBound(object sender,
        System.Web.UI.WebControls.DataGridItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item ||  e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList d = (DropDownList)e.Item.FindControl("NameOfYourDDL");
//Then you can fetch the value. PS! I dont know which cell your data lies
string ValueToSetInDDL = e.Item.Cells[5].Text;


d.SelectedValue = ValueToSetInDDL
}

}
0
RejojohnyCommented:
using itemdatabound event

from
http://authors.aspalliance.com/aldotnet/examples/datagrideditddlvb.txt

Public Sub DataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
      'Determine the ListItem Type
      Dim itemType As ListItemType = e.Item.ItemType

      'We only want to run this code for items in edit mode
      If (itemType = ListItemType.EditItem) Then
          'Locate our DropDownList
          Dim myList As DropDownList = CType(e.Item.FindControl("Dropdownlist1"), DropDownList)

          'Grab the current row of data - we need that so we know what value to select in the ddl
          Dim myRow As DataRowView = CType(e.Item.DataItem, DataRowView)

          'Grab the value we want to select from the current row of data in our DataGrid/List/Repeater
          Dim myRole As String = myRow("RoleLevel").ToString()
          
            'Set the Text and Value field for our DropDownList
          myList.DataTextField = "RoleName"
            myList.DataValueField = "RoleID"
        
            'Fill our DropDownList with the standard list of data
          myList.DataSource = GetRoles()
          myList.DataBind()

          'Use the FindByValue function to locate the item we want to select
            'There is also a FindByText function that can be used in the same way
          myList.Items.FindByValue(myRole).Selected = true
        End If
    End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.