Click here to Skip to main content
13,597,485 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        grdEmployee.DataSource = ds.Tables["Employee"];
        grdEmployee.DataBind();
    }

    protected void grdEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
            Label lblid = (Label)e.Row.FindControl("lbldept");
            DataSet dsr = new DataSet();
            dsr.ReadXml(Server.MapPath("Dept.xml"));
            ddlDepartment.DataSource = dsr;
            
            //ddlDepartment.DataTextField = "Dname";
            //ddlDepartment.DataValueField = "Deptid";
            ddlDepartment.DataBind();

            string id = lblid.Text;
            ddlDepartment.Items.FindByValue(id).Selected = true;
            ddlDepartment.Items.Insert(0, "-Select Department-");

           // ddlDepartment.SelectedValue = e.Row.Cells[0].Text;
            
            
              
        }   
    }
}
aspx page:

<asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false"

            onrowdatabound="grdEmployee_RowDataBound" Height="273px" Width="455px">
            <Columns>
            <asp:TemplateField>
                    <ItemTemplate>
                       <asp:Label ID="lbldept" runat="server" Text='<%#Bind("deptid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlDepartment" DataValueField="value" DataTextField="name" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="name" />
                <asp:BoundField DataField="sal" HeaderText="sal" />
                <asp:BoundField DataField="Deptid" HeaderText="Deptid" ControlStyle-Width="0px" />
            </Columns>
        </asp:GridView>

in code behind i have written two times ds.readxml beacuse one emp.xml is binding grid and another one is binding with drop downlist.how can i place at one place i have two xml files.dept.xml is binding with drop downlist which is inside a gridview.MY assignment is write to place one place it because no need to write two times ds.readxml.How will be done ?pls anyone help me
Posted 7-Feb-12 1:58am
Updated 7-Feb-12 8:27am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is trivial. It will take two seconds. Make the dataset a member variable and read it in the page load only.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You do not have to load the dsr DataSet for every row.
Can you try this code -

DataSet ds = new DataSet();
DataSet dsr = new DataSet();
protected void Page_Load(object sender, EventArgs e)
    {
        ds.ReadXml(Server.MapPath("emp.xml"));
        dsr.ReadXml(Server.MapPath("Dept.xml"));

        grdEmployee.DataSource = ds.Tables["Employee"];
        grdEmployee.DataBind();
    }
 
    protected void grdEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
            Label lblid = (Label)e.Row.FindControl("lbldept");
            ddlDepartment.DataSource = dsr;
            
            //ddlDepartment.DataTextField = "Dname";
            //ddlDepartment.DataValueField = "Deptid";
            ddlDepartment.DataBind();
 
            string id = lblid.Text;
            ddlDepartment.Items.FindByValue(id).Selected = true;
            ddlDepartment.Items.Insert(0, "-Select Department-");
 
           // ddlDepartment.SelectedValue = e.Row.Cells[0].Text;
        }   
    }
}


Please vote the solution if you like it.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03-2016 | 2.8.180621.3 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100