Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to bind dropdown value to datagrid?

Posted on 2012-12-23
4
Medium Priority
?
313 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

782 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