Solved

how to bind dropdown value to datagrid?

Posted on 2012-12-23
4
306 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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