Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
XML
<asp:GridView ID="xmlGrid" runat="server" AutoGenerateColumns="False" BackColor="White"
                BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="336px"
                Height="160px" onrowdatabound="xmlGrid_RowDataBound"
                onselectedindexchanged="xmlGrid_SelectedIndexChanged">
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#330099" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                <SortedDescendingHeaderStyle BackColor="#7E0000" />
                <Columns>
                    <asp:TemplateField HeaderText="EmployeeID">
                        <ItemTemplate>
                            <asp:Label ID="empId" runat="server" Text='<%#Eval("Id")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="EmployeeName" DataField="Name" />
                    <asp:TemplateField >
                    <HeaderTemplate>Age</HeaderTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="empAge" runat="server"  Width="100%" DataSource='<%#Eval("age")%>'>
                            </asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="empAge" runat="server" DataSource='<%#Eval("age")%>'>
                            <asp:ListItem>21</asp:ListItem>
                            <asp:ListItem>22</asp:ListItem>
                            <asp:ListItem>23</asp:ListItem>
                            <asp:ListItem>24</asp:ListItem>
                            <asp:ListItem>25</asp:ListItem>
                            <asp:ListItem>26</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ButtonType="Link" ShowEditButton="true" />
                </Columns>
            </asp:GridView>

DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/xmlData.xml"));
xmlGrid.DataSource = ds;
xmlGrid.DataBind();
Posted
Comments
Prasad CM 13-Jun-11 5:25am    
Post Ur Xml File..

Is EmpID Value Loading Correctly ?..
Abhi KA 13-Jun-11 5:44am    
<employees>
<employee>
<id>1
<name>Raju
<age>25

<employee>
<id>2
<name>Kumar
<age>26

<employee>
<id>3
<name>rajesh
<age>28

<employee>
<id>4
<name>somesh
<age>26

Prasad CM 13-Jun-11 5:38am    
Ok I Got Solution for Ur Question
Just Look Out Solution 1
And Replay Me About Solution 1

I think you should check xmlData.xml data again.

maybe the .xml file not match your columns in gridview.
 
Share this answer
 
Comments
Abhi KA 13-Jun-11 5:44am    
get age values but example 25
2 one value
5 one value
it show two values
Instead Of This One
<asp:dropdownlist id="empAge" runat="server" width="100%" datatextfield="<%#Eval("age")%>" >

U Can Directly Use
<%#Eval("age")%>

So That Age Will Load for Each Employee
 
Share this answer
 
v2
Comments
Prasad CM 13-Jun-11 6:50am    
Update That Should Happen
After U Click On Edit Button Right?
SO First Display Initial Value in ItemTemplate
Later in EditItemTemplate U can Load All Values..
Abhi KA 13-Jun-11 7:51am    
its working thnq u
Prasad CM 15-Jun-11 1:54am    
Then Accept this Solution..
Here Is the Solution...

Change In ItemTemplate
DataSource='<%#Eval("age")%>'

To
DataTextField='<%#Eval("age")%>'

Like This...
<itemtemplate>
  <asp:dropdownlist id="empAge" runat="server" width="100%" 
        DataTextField='<%#Eval("age")%>'>  
</itemtemplate>
 
Share this answer
 
v4
Comments
Abhi KA 13-Jun-11 5:43am    
DataTextField='<%#Eval("age")%>'> its not show any data
Hi,

Dropdown datasource should be as collection object or datatable.
Here you cant assign the column name as datasource.
Or you can call the one function from code behind which will return collection object.

Regards,
Kiran.
 
Share this answer
 
Hi.

I think you should bind your DropDownList to a proper datasource, not use DataSource='<%#Eval("age")%>'.

Maybe this will help (from MSDN[^]):



ASP
<asp:DropDownList id="ColorList"
     AutoPostBack="True"
     OnSelectedIndexChanged="Selection_Change"
     runat="server"/>


C#
void Selection_Change(Object sender, EventArgs e)
{
   // Enter here what needs to happen when the dropdown selected item has been changed
}


But more importantly:

C#
void Page_Load(Object sender, EventArgs e)
{

   // Load data for the DropDownList control only once, when the
   // page is first loaded.
   if(!IsPostBack)
   {

      // Specify the data source and field names for the Text
      // and Value properties of the items (ListItem objects)
      // in the DropDownList control.
      ColorList.DataSource = CreateDataSource();
      ColorList.DataTextField = "ColorTextField";
      ColorList.DataValueField = "ColorValueField";

      // Bind the data to the control.
      ColorList.DataBind();

      // Set the default selected item, if desired.
      ColorList.SelectedIndex = 0;

   }

}

And
C#
ICollection CreateDataSource()
     {

        // Create a table to store data for the DropDownList control.
        DataTable dt = new DataTable();

        // Define the columns of the table.
        dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
        dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

        // Here you can read the values from the DataGrid or from the XML file
        // Populate the table with sample values.
        dt.Rows.Add(CreateRow("White", "White", dt));
        dt.Rows.Add(CreateRow("Silver", "Silver", dt));
        dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));
        dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));
        dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));

        // Create a DataView from the DataTable to act as the data source
        // for the DropDownList control.
        DataView dv = new DataView(dt);
        return dv;
     }

     DataRow CreateRow(String Text, String Value, DataTable dt)
     {
        // Create a DataRow using the DataTable defined in the
        // CreateDataSource method.
        DataRow dr = dt.NewRow();

        // This DataRow contains the ColorTextField and ColorValueField
        // fields, as defined in the CreateDataSource method. Set the
        // fields with the appropriate value. Remember that column 0
        // is defined as ColorTextField, and column 1 is defined as
        // ColorValueField.
        dr[0] = Text;
        dr[1] = Value;

        return dr;
     }


In CreateDataSource() you can read the values from file or the DataGrid and assign them to a datasource.
 
Share this answer
 

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