Solved

how to bind dropdown value to datagrid?

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now