Solved

how to bind dropdown value to datagrid?

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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