This is a migrated thread and some comments may be shown as answers.

populate DDL in radgird with database value

3 Answers 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 15 Mar 2012, 06:46 PM
Ok, I have a grid with a couple of dropdownlist.  I have been able to populate them but what I cannot seem to get is to load the correct value from the database into the DDL on time of grid binding.  Right now the ddl's populate with all the first value and I want to populate them with the value from the DB.  I tried binding but does not seem to work.

<ItemTemplate>
                                    <asp:DropDownList ID="ddlFUndCode" runat="server" DataValueField='<%# Bind("intFundCodeId") %>'></asp:DropDownList>
                                </ItemTemplate>

<Columns>
                             <telerik:GridTemplateColumn HeaderText="PO NUM">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtPONum" runat="server" Text='<%# Bind("strPoNum") %>'></asp:TextBox>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridDropDownColumn DataField="intMakeId" UniqueName="Make"></telerik:GridDropDownColumn>
                            <telerik:GridTemplateColumn HeaderText="MAKE">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlmake" runat="server"></asp:DropDownList>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="COST">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtCost" runat="server" Text='<%# Bind("fltCost") %>'></asp:TextBox>
                                    <asp:FilteredTextBoxExtender ID="txtCost_FTE" runat="server" Enabled="True" TargetControlID="txtCost" ValidChars="." FilterType="Custom, Numbers" />
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="FUND CODE">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlFUndCode" runat="server" DataValueField='<%# Bind("intFundCodeId") %>'></asp:DropDownList>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="ASSET TAG">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtAssetNum" runat="server" Text='<%# Bind("strAssetNum") %>'></asp:TextBox>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="SN">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtSN" runat="server" Text='<%# Bind("strSn") %>'></asp:TextBox>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                        </Columns>
  
  
 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            myRadGrid.DataBind()
        End If
    End Sub
  
    Protected Sub myRadGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
        If (TypeOf e.Item Is GridDataItem) Then
            Dim make As DropDownList = e.Item.FindControl("ddlmake")
            Dim Code As DropDownList = e.Item.FindControl("ddlFUndCode")
  
            sql = "Select intMakeId, strmake from tblmake"
            buildDD(sql, make)
  
            sql = "Select intFundCodeId, Cast(intCodeNUm as varchar) + ' \ ' + strDescription from tblFundCodes"
            buildDD(sql, Code)
  
        End If
    End Sub
  
    Protected Sub myRadGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles myRadGrid.NeedDataSource
        sql = "Select intRecId, strPoNum, intMakeId, fltCost, intFundCodeId, strAssetNum, strSn From tblReceived"
  
        myRadGrid.DataSource = getData(sql)
  
    End Sub

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 16 Mar 2012, 07:22 AM
Hello Kevin,

You can access the DropDownList and bind the selected value using DataBinder property from code behind.
aspx:
<telerik:GridTemplateColumn HeaderText="MAKE">
  <ItemTemplate>
      <asp:DropDownList ID="ddlmake" runat="server">
      </asp:DropDownList>
 </ItemTemplate>
</telerik:GridTemplateColumn>
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 if ((e.Item is GridDataItem))
{
 DropDownList make = (DropDownList)e.Item.FindControl("ddlmake");
 string updateQuery = "SELECT FirstName FROM [Employees]";
 SqlCommand.CommandText = updateQuery;
 SqlCommand.Connection = conn;
 SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
 
DataSet ds = new DataSet();
 da.Fill(ds);
 make.DataSource = ds;
make.DataTextField = "FirstName";
make.DataValueField = "FirstName";
make.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "FirstName").ToString();
make.DataBind();
 }
}

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 16 Mar 2012, 07:48 PM
Hi,

ok I am already doing this per my code, what I have not figured out how to do is set it to the same ID as what is in the datatable.  I am loading this grid as an edit grid adn what is coming in is the Id in the database, so the ddl needs to be set to that id and not the default ID from the initiall ddl load.  So when I pull up the page if the ddl has ID loaded that are 1 - 8 and when that row loads from teh database the Id from the table is 6, i need the dropdownlist to be set to 6 and not 1.
0
Shinu
Top achievements
Rank 2
answered on 19 Mar 2012, 01:18 PM

Hi Kevin,

The above code is working fine for me and it is getting  corresponding SelectedValue in DropDownList using DataBinder property.  Can you please paste your entire code (code inside function  buildDD(sql, make) to identify the real issue?

Thanks,
Shinu.

Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Share this question
or