locked
Dropdownlist data to gridview in asp.net RRS feed

  • Question

  • User-1447786422 posted

    I Have a dropdownlist name dllGioiTinh and it content two gender "male" and "female" with the database i create in sqlserver, now i want use my dropdownlist data to gridview so when i click update it show my gender into gridview, pls help.

    //My dataconection
    private void LoadDataToDropDownList()
            {
                if (sqlCon.State == ConnectionState.Closed)
                    sqlCon.Open();
                DataTable dataTable = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter("select GioiTinh=(case GioiTinh WHEN 'true' THEN N'Nam' WHEN 'false' THEN N'Nữ' END) from SinhVien", sqlCon);
                adapter.Fill(dataTable);
                ddlGioiTinh.Items.Clear();
                ddlGioiTinh.SelectedIndex = -1;
                ddlGioiTinh.SelectedValue = null;
                ddlGioiTinh.ClearSelection();
                ddlGioiTinh.DataSource = dataTable;
                ddlGioiTinh.DataTextField = "GioiTinh";
                ddlGioiTinh.DataValueField = "GioiTinh";
                ddlGioiTinh.DataBind();
                ddlGioiTinh.Items.Insert(0, (new ListItem("---Chọn Giới Tính---", "-1")))
            }
    
    //Update button
    
    protected void lnk_OnClick(object sender, EventArgs e)
            {
                int MaSV = Convert.ToInt32((sender as LinkButton).CommandArgument);
                if (sqlCon.State == ConnectionState.Closed)
                    sqlCon.Open();
                SqlDataAdapter sqlDa = new SqlDataAdapter("XemTheoMaSV", sqlCon);
                sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
                sqlDa.SelectCommand.Parameters.AddWithValue("@MaSV", MaSV);
                DataTable dtbl = new DataTable();
                sqlDa.Fill(dtbl);
                sqlCon.Close();
                hfSinhVien.Value = MaSV.ToString();
                txtHoTen.Text = dtbl.Rows[0]["HoTen"].ToString();
                txtNgaySinh.Text = dtbl.Rows[0]["NgaySinh"].ToString();
                ddlGioiTinh.SelectedValue = dtbl.Rows[0]["GioiTinh"].ToString();
                txtQueQuan.Text = dtbl.Rows[0]["QueQuan"].ToString();
                txtSDT.Text = dtbl.Rows[0]["SDT"].ToString();
                txtEmail.Text = dtbl.Rows[0]["Email"].ToString();
                txtLop.Text = dtbl.Rows[0]["Lop"].ToString();
                btnSave.Text = "Update";
                btnDelete.Enabled = true;
            }

    Wednesday, July 18, 2018 2:57 AM

Answers

  • User-1171043462 posted

    Check this sample. Created using my article: Store (Save) GridView data in ViewState in ASP.Net using C# and VB.Net

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
            EmptyDataText="No records has been added.">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
                <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />
            </Columns>
        </asp:GridView>
        <br />
        <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
            <tr>
                <td style="padding-bottom: 10px">
                    Name:<br />
                    <asp:TextBox ID="txtName" runat="server" />
                </td>
            </tr>
            <tr>
                <td style="padding-bottom: 10px">
                    Country:<br />
                    <asp:DropDownList ID = "ddlCountries" runat="server">
                        <asp:ListItem Text="United States" Value = "United States" />
                        <asp:ListItem Text="India" Value = "India" />
                        <asp:ListItem Text="France" Value = "France" />
                        <asp:ListItem Text="Russia" Value = "Russia" />
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
                </td>
            </tr>
        </table>
        </form>
    </body>
    </html>
    
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
                ViewState["Customers"] = dt;
                this.BindGrid();
            }
        }
    
        protected void BindGrid()
        {
            GridView1.DataSource = (DataTable)ViewState["Customers"];
            GridView1.DataBind();
        }
    
        protected void Insert(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Customers"];
            dt.Rows.Add(txtName.Text.Trim(), ddlCountries.SelectedItem.Value);
            ViewState["Customers"] = dt;
            this.BindGrid();
            txtName.Text = string.Empty;
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2018 7:48 AM

All replies

  • User-1171043462 posted

    You want to Filter GridView data?

    Wednesday, July 18, 2018 7:05 AM
  • User-1447786422 posted

    No, what i mean is when i click update, i want my selected value in dropdownlist apear in my grid view.

    Thursday, July 19, 2018 1:15 AM
  • User-330142929 posted

    Hi Tuan1296,

    According to your description, I am not clear about what you really want.

    In my opinion, the first possibility is that there is a dropdownlist outside of the gridview and a button. After selecting the dropdownlist option, clicking the button will update a row of data in the gridview.

    And the second possibility is that you use the dropdownlist to display and update the data in a column of data in the gridview. After the selection, the records in the database table will be updated.

    Rather than letting me guess your code, you should streamline your code and submit your front-end code. It is easily to give you an effective reply if you could post more details about your requirement.

    Best Regards

    Abraham

    Friday, July 20, 2018 1:51 AM
  • User-1171043462 posted

    Still not clear. Do you have a Form below Grid? And you want to add DropDownList Selected value as Row in GridView?

    Friday, July 20, 2018 6:48 AM
  • User-1447786422 posted

    Yes, your first opinion is right, can u help me?

    Monday, July 23, 2018 1:08 AM
  • User-1447786422 posted

    what i want is when i click update button, the selected value in dropdownlist apear in my gridview, pls help

    Monday, July 23, 2018 1:14 AM
  • User-330142929 posted

    Hi Tuan1296,

    Ok. I get your point. But how do I know which line to update? Or only one line of data. if you just want to display the result in the gridview, I think we could construct a Datatable and then bind the datasource to the gridview. If you want to update one of the columns by conditions(CRUD).

    you could refer to this good answer.

    https://forums.asp.net/t/2143518.aspx?Gridview+CRUD

    Feel free to let me know if you have any questions.

    Best Regards
    Abraham

    Monday, July 23, 2018 3:29 AM
  • User-1171043462 posted

    Check this sample. Created using my article: Store (Save) GridView data in ViewState in ASP.Net using C# and VB.Net

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
            EmptyDataText="No records has been added.">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
                <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />
            </Columns>
        </asp:GridView>
        <br />
        <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
            <tr>
                <td style="padding-bottom: 10px">
                    Name:<br />
                    <asp:TextBox ID="txtName" runat="server" />
                </td>
            </tr>
            <tr>
                <td style="padding-bottom: 10px">
                    Country:<br />
                    <asp:DropDownList ID = "ddlCountries" runat="server">
                        <asp:ListItem Text="United States" Value = "United States" />
                        <asp:ListItem Text="India" Value = "India" />
                        <asp:ListItem Text="France" Value = "France" />
                        <asp:ListItem Text="Russia" Value = "Russia" />
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
                </td>
            </tr>
        </table>
        </form>
    </body>
    </html>
    
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
                ViewState["Customers"] = dt;
                this.BindGrid();
            }
        }
    
        protected void BindGrid()
        {
            GridView1.DataSource = (DataTable)ViewState["Customers"];
            GridView1.DataBind();
        }
    
        protected void Insert(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Customers"];
            dt.Rows.Add(txtName.Text.Trim(), ddlCountries.SelectedItem.Value);
            ViewState["Customers"] = dt;
            this.BindGrid();
            txtName.Text = string.Empty;
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2018 7:48 AM