locked
Bind data to dropdownlist and selectedvalue by C# RRS feed

  • Question

  • User-2010019558 posted

     have two table:

    Category : CatID, CatName
    News : NewsID, CatID,Title,Content

    Now, i want, when i edit on table NewS, i have dropdownlist and bind all values from table Category and show SelectedValue previous

    Please help me

    Wednesday, May 8, 2019 8:22 AM

All replies

  • User288213138 posted

    Hi hoailinh,
     
      According to your description, I have made a demo for your reference.If you want to show SelectedValue previous, you can do that with SelectValue.


    The code:

    Aspx:
    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing" >
                    <Columns>
                        <asp:TemplateField HeaderText="NewsID" ItemStyle-Width="150">
                            <ItemTemplate>
                                <%# Eval("NewsID") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="NewsID" runat="server" Text='<%# Eval("NewsID") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                      
    
                        <asp:TemplateField HeaderText="CatID" ItemStyle-Width="150">
                            <ItemTemplate>
                                <%# Eval("CatID") %>
                            </ItemTemplate>
                            <EditItemTemplate>
    
                                <asp:DropDownList ID="CatID" runat="server" DataTextField="CatName" DataValueField="CatId" DataSourceID="SqlDataSource1" SelectedValue='<%# Bind("CatID") %>'  >
                                    
                                </asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Title" ItemStyle-Width="150">
                            <ItemTemplate>
                                <%# Eval("Title") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="Title" runat="server" Text='<%# Eval("Title") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Content" ItemStyle-Width="150">
                            <ItemTemplate>
                                <%# Eval("Content") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="Content" runat="server" Text='<%# Eval("Content") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
    
    
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
                            </ItemTemplate>                        
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                  <asp:HiddenField runat="server" Value="10"></asp:HiddenField>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:2019/4/22ConnectionString %>" SelectCommand="select * from category"></asp:SqlDataSource>
            </div>
        </form>
    
    Aspx.cs:
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("NewsID"), new DataColumn("CatID"), new DataColumn("Title"), new DataColumn("Content") });
                    dt.Rows.Add(1, "catid1", "Title1", "Content1");
                    dt.Rows.Add(2, "catid2", "Title2", "Content2");
                    dt.Rows.Add(3, "catid3", "Title3", "Content3");
                    ViewState["dt"] = dt;
                    this.BindGrid();
                }
                         
            }
            protected void BindGrid()
            {
                GridView1.DataSource = ViewState["dt"] as DataTable;
                GridView1.DataBind();
            }
            protected void OnRowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;         
                this.BindGrid();
         
    
    
            }
    
            protected void OnUpdate(object sender, EventArgs e)
            {
                GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
                string name = (row.FindControl("NewsID") as TextBox).Text;
                string title = (row.FindControl("Title") as TextBox).Text;
                string content = (row.FindControl("Content") as TextBox).Text;
                string catid = (row.FindControl("CatID") as DropDownList).SelectedItem.Value;
                
                DataTable dt = ViewState["dt"] as DataTable;
                dt.Rows[row.RowIndex]["NewsID"] = name;
                dt.Rows[row.RowIndex]["Title"] = title;
                dt.Rows[row.RowIndex]["Content"] = content;
                dt.Rows[row.RowIndex]["CatID"] = catid;
                ViewState["dt"] = dt;
                GridView1.EditIndex = -1;
                this.BindGrid();
            }
    
            protected void OnCancel(object sender, EventArgs e)
            {
                GridView1.EditIndex = -1;
                this.BindGrid();
            }

    The result:

    Best Regards,

    Sam

    Wednesday, May 8, 2019 11:54 AM