Solved

how to bind dropdown value to datagrid?

Posted on 2012-12-23
4
308 Views
Last Modified: 2012-12-27
Hi,

I have 2 dropdowns in my screen with "Sector" and "NewsSectorType". when i select values from these 2 dropdowns, after click on Add button i want to bind selected values to gridview.
So my gridview will have only 2 columns.  

First dropdown values i get from database from one column of one table. Second dropdown values are hardcoded values.

How to do this? please find the attached file for more information.


Thanks,
binding-to-grid-from-dropdown.jpg
0
Comment
Question by:swathi111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 38717543
To update this kind of values, it is better you use update panel. So the entire page will not be post back.
To get your output just follow these steps
1. Select the values from DropDown.
2. Click on Add Button.
3. In add function, you are expecting to store the values in Database.
4. After storing values to Database. Again Bind the GridView in Add Button Click event
GridView1.DataBind();
0
 
LVL 15

Accepted Solution

by:
santhimurthyd earned 500 total points
ID: 38718711
Try with the below

UI Code
 <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
     <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <table>
                <tr>
                    <td>
                        <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="false">
                            <asp:ListItem Text="Sector1" Value="Sector1"></asp:ListItem>
                            <asp:ListItem Text="Sector2" Value="Sector2"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddl2" runat="server" AutoPostBack="false">
                            <asp:ListItem Text="Singapore" Value="Singapore"></asp:ListItem>
                            <asp:ListItem Text="General" Value="General"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td><asp:Button ID="btnAdd" Text="Add" runat="server" onclick="btnAdd_Click" /></td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:GridView ID="grdDetails" runat="server" AutoGenerateColumns="false" EnableViewState="true">
                            <Columns>
                                <asp:BoundField DataField="Sector" HeaderText="Sector" />
                                <asp:BoundField DataField="NewSector" HeaderText="News Sector Type" />
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </ContentTemplate>
     </asp:UpdatePanel>
    </div>
    </form>

Open in new window


Code Behind
 public partial class _Default : System.Web.UI.Page
    {
        DataTable dtSectors;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            CreateDataSource();
            if (grdDetails.Rows.Count > 0)
            {
                LoadDataBackFromGrid();
            }
            
            AddNewRow();

            grdDetails.DataSource = null;
            grdDetails.DataSource = dtSectors;
            grdDetails.DataBind();
        }

        private void AddNewRow()
        {
            DataRow dr = dtSectors.NewRow();
            dr["Sector"] = ddl1.SelectedItem.Text;
            dr["NewSector"] = ddl2.SelectedItem.Text;
            dtSectors.Rows.Add(dr);
        }

        private void LoadDataBackFromGrid()
        {
            DataRow dr;
            foreach (GridViewRow row in grdDetails.Rows)
            {
                dr = dtSectors.NewRow();
                for (int i = 0; i < dtSectors.Columns.Count; i++)
                {
                    dr[i] = row.Cells[i].Text;
                }
                dtSectors.Rows.Add(dr);
            } 

        }

        private void CreateDataSource()
        {
            dtSectors = new DataTable();
            dtSectors.Columns.Add("Sector", typeof(System.String));
            dtSectors.Columns.Add("NewSector", typeof(System.String));
        }
    }

Open in new window

0
 

Author Comment

by:swathi111
ID: 38723054
Hi santhimurthyd,

Sorry for late reply due to my system corrupted.

I just tried your code everything is working but one issue. When I add 2nd row first row is not displaying in grid. For example, I added 3 rows then first 2 rows are not displaying in the gird. Only 3rd row is displaying. Pls see the image to understand clearly. first row is empty when i add 2nd row.

Please find the attached image to see output.

I am currently working on it to figure out the issue, if you have any idea pls let me know

Really thanks alot for your help.




Thanks,
Swathi
output.jpg
0
 

Author Comment

by:swathi111
ID: 38723949
Please find the attached file. It is showing error when i am posting this comment.
changed-code.txt
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question