We help IT Professionals succeed at work.

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

vandy02
vandy02 asked
on
Medium Priority
712 Views
Last Modified: 2007-12-19
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!
Comment
Watch Question

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
}

}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
e.Item.Cells[5]
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
}

}
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.