How to load dropdown values inside gridview or Listview using DataTable

BeUnique 2,252 Reputation points
2025-01-06T16:45:20.6533333+00:00

i'm using dropdown values inside modal popup. whenever i am selecting some values in dropdown,

i want to assign those values in datatable.

Finally, i want to assign datatable values into either gridview or Listview.

Is it possible to load values in gridview from Datatable which is getting from dropdown values....?

DropDown values

User's image

Note : When loading the page, the same should check from SQL table. if Record not exists, then it should add from the dropdown values.

How to do this....?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,561 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,185 questions
{count} votes

Accepted answer
  1. XuDong Peng-MSFT 10,931 Reputation points Microsoft Vendor
    2025-01-07T03:54:39.6866667+00:00

    Hi @BeUnique

    Is it possible to load values in gridview from Datatable which is getting from dropdown values....?

    If you need to implement this requirement, you only need to define the SelectedIndexChanged event of the DropDownList control, get the value and text of the selected option in this event, add it to the DataTable based on certain judgment conditions, and finally bind the data source of the Gridview or ListView you mentioned.

    This is a simple demonstration, which may lack some conditional judgments. You need to add or delete them according to your needs:

    Index.aspx
    
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList runat="server" ID="DDL1" 
                OnSelectedIndexChanged="DDL1_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem Text="ProdName" Value="ProdId"></asp:ListItem>
                <asp:ListItem Text="ToothPaste" Value="101"></asp:ListItem>
                <asp:ListItem Text="Detergent" Value="102"></asp:ListItem>
                <asp:ListItem Text="Choco" Value="103"></asp:ListItem>
                <asp:ListItem Text="MicroPaper" Value="104"></asp:ListItem>
            </asp:DropDownList>
            <hr />
            <asp:GridView ID="GV1" runat="server">
    
            </asp:GridView>
        </div>
    </form>
    
    Index.aspx.cs
    
    public partial class Index : System.Web.UI.Page
    {
        private static DataTable dt;
        private static bool DataBaseIsEmpty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dt = getDataResource();
                GridViewBind();
            }
        }
    
        private void GridViewBind()
        {
            GV1.DataSource = dt;
            GV1.DataBind();
        }
    
        private DataTable getDataResource()
        {
            dt = new DataTable();
            //get resource from SQL DataBase, just for test below
            dt.Columns.Add("ProdId", typeof(int));
            dt.Columns.Add("ProdName", typeof(string));
            //dt.Rows.Add(105,"ProdName2");
            //dt.Rows.Add(106,"ProdName3");
            //dt.Rows.Add(107,"ProdName4");
            if (dt.Rows.Count == 0) {
                DataBaseIsEmpty = true;
            }else
            {
                DataBaseIsEmpty = false;
            }
            return dt;
        }
    
        protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDL1.SelectedIndex != 0)
            {
                int value = int.Parse(DDL1.SelectedItem.Value);
                string text = DDL1.SelectedItem.Text;
                if (DataBaseIsEmpty)
                {
                    dt.Rows.Add(value, text);
                }
                GridViewBind();
            }
        }
    }
    

    Or if you have other needs, please clarify more details.

    Best Regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 29,281 Reputation points
    2025-01-07T17:31:22.44+00:00

    You should take the time to learn the GridView data bound control.

    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview?view=netframework-4.8.1

    suppose, we want to add or delete some specific record in gridview, then we want to delete in database as well as DataTable - How to do this...?

    The official documentation covers delete.

    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowdeleting?view=netframework-4.8.1

    we want to add or delete only in DataTable -- How to do this...?

    This is not a standard design approach. The standard approach is invoking CRUD operations in the database, then populating a DataTable (GridView's data source) from the database, finally data bind the GridView using the DataTable.

    The actual implementation depends on your needs. For example, if you have simple table updates then every thing can be done using the editor with little to no coding. If your requirements are more complex then you get to code the logic however you like.

    If you need help with your code then share your code otherwise we have no idea what you're doing.

    Lastly, there are tons of GridView CRUD tutorial online that you can study.

    https://www.google.com/search?q=GridView+CRUD

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.