• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

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!
0
vandy02
Asked:
vandy02
  • 2
2 Solutions
 
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now