Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While Binding a DataSource to an DropDownList in the Grid View at Run time I get the Following error specifying that

" DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Dept_ID'. "

I had used the following code.. Please Its Urgent My Dear Friends...

Regards
Sushil Dharmar

.aspx Page

XML
<asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="False" BackColor="White"
                BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowDataBound="grdEmployee_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="EMP_ID" HeaderText="EMP_ID" />
                    <asp:BoundField DataField="EMP_NAME" HeaderText="EMP_NAME" />
                    <asp:BoundField DataField="SALARY" HeaderText="SALARY" />
                    <asp:BoundField DataField="HIRE_DATE" HeaderText="HIRE_DATE" />
                    <asp:TemplateField HeaderText="DEPT_ID">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlDeptId" runat="server" AutoPostBack="True">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                <RowStyle BackColor="White" ForeColor="#003399" />
                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                <SortedDescendingHeaderStyle BackColor="#002876" />
            </asp:GridView>




.Cs Page

C#
protected void Bind()
   {
       _str = " SELECT EMP_ID,EMP_NAME,SALARY,HIRE_DATE from Employee ";
       adp = new SqlDataAdapter(_str, con);
       ds = new DataSet();
       adp.Fill(ds);
       if (ds.Tables[0].Rows.Count > 0)
       {
           grdEmployee.DataSource = ds.Tables[0];
           grdEmployee.DataBind();
       }
   }



C#
protected void grdEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               DropDownList ddlDeptId = (DropDownList)e.Row.FindControl("ddlDeptId");
               _strdrop = " SELECT distinct Dept_ID from Department ";
               adpdrp = new SqlDataAdapter(_strdrop, con);
               dsdrp = new DataSet();
               adpdrp.Fill(dsdrp);
               ddlDeptId.DataSource = dsdrp;
               ddlDeptId.DataTextField = "Dept_ID";
               ddlDeptId.DataValueField = "Dept_ID";
               ddlDeptId.ID = "Dept_ID";
               ddlDeptId.DataBind();
           }
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }
   }
Posted
Updated 10-Apr-12 16:15pm
v2

1 solution

hi ,
try this
C#
ddlDeptId.DataSource = ds.Tables[0];
             ddlDeptId.DataTextField = "Dept_ID";
             ddlDeptId.DataValueField = "Dept_ID";
             ddlDeptId.ID = "Dept_ID";
             ddlDeptId.DataBind();

best regards
M.Mitwalli
 
Share this answer
 
Comments
SushilDharmar 8-Apr-12 4:37am    
It Not Working yar..
Still the Same Error...
SushilDharmar 8-Apr-12 5:08am    
I Got It yar.. The Problem is been Solved out... :-)
Thank you So much for your Help Friend...
Mohamed Mitwalli 8-Apr-12 5:13am    
your welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900