locked
gridview not display in the output page ? RRS feed

  • Question

  • User-1026236167 posted

    hello my gridview not display in the output page please solve this 

    cs

    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace Store
    {
    public partial class WebForm5 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();

    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }

    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    FillGridview1();
    }

    public void FillGridview1()
    {
    SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_products", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
    try
    {
    ((DropDownList)GridView2.Rows[i].FindControl("dd3")).DataSource = DT;
    ((DropDownList)GridView2.Rows[i].FindControl("dd3")).DataBind();
    ((DropDownList)GridView2.Rows[i].FindControl("dd3")).Items.Insert(0, new ListItem() { Text = "Select", Value = "0" });
    }
    catch { }
    }

    //dd3.DataSource = DT;
    //dd3.DataBind();
    //dd3.Items.Insert(0, new ListItem() { Text = "Select" });

    }

    protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
    {
    foreach (GridViewRow gr in GridView2.Rows)
    {
    string benefit = "0";
    TextBox tb = (TextBox)gr.FindControl("txt1");
    benefit = ((tb.Text));

    TextBox bn29 = ((TextBox)gr.FindControl("txt1"));
    bn29.Text = benefit;

    foreach (GridViewRow gr7 in GridView2.Rows)
    {
    string benefit7 = "0";
    DropDownList tb7 = (DropDownList)gr7.FindControl("dd3");
    benefit7 = ((tb7.Text));

    DropDownList bn22 = ((DropDownList)gr7.FindControl("dd3"));
    bn22.Text = benefit7;


    bn29.Text = bn22.SelectedValue.ToString();
    }
    }
    }

    }
    }

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Store.WebForm5" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" CellPadding="4"
    ForeColor="#333333" GridLines="None" Width="186px">
    <AlternatingRowStyle BackColor="White" />
    <Columns>

    <asp:TemplateField HeaderText="hello">
    <ItemTemplate>
    <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px"
    OnSelectedIndexChanged="dd3_SelectedIndexChanged" DataTextField="mrp"
    DataValueField="mrp" AutoPostBack="true"></asp:DropDownList>
    </ItemTemplate>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="hello">

    <ItemTemplate>

    </ItemTemplate>
    </asp:TemplateField>


    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>


    </div>
    </form>
    </body>
    </html>

    Monday, August 24, 2020 5:48 AM

Answers

  • User-939850651 posted

    Hi prabhjot1313.

    I used the code you provided to create a simple example.

    I found that you did not set a data source for the grid view, so the grid will not display any data on the page.

    public void FillGridview1()
            {
                SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_products", connection);
                DataTable DT = new DataTable();
                adp.Fill(DT);
                GridView2.DataSource = DT;
                GridView2.DataBind();
                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    try
                    {
                        ((DropDownList)GridView2.Rows[i].FindControl("dd3")).DataSource = DT;
                        ((DropDownList)GridView2.Rows[i].FindControl("dd3")).DataBind();
                        ((DropDownList)GridView2.Rows[i].FindControl("dd3")).Items.Insert(0, new ListItem() { Text = "Select", Value = "0" });
                    }
                    catch {
                    //do something here
                    }
                }
            }

    Pelase bind data source to gridview before iterate the rows of the GridView.

    On the other hand, I recommend that you could learn to use debugging tools.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2020 8:52 AM