Display Particular Data in Gridview without using SQL Query ( Select * From TableName )


Display Particular Data in Gridview without using sql query

In this articles i'm going to explain about how to Bind Data from SQL Server to Gridview, we already know how to bind SQL Server - to - gridview. After binding bunch of records data into gridview and if their is a requirement to get particular records and display in gridview. Again we have to write SQL Query (i.e SELECT * FROM TABLENAME WHERE USERD=@userid) to get that records.
Instead of Writing SQL Query we can get particular records and we can display in gridview as i show in above image. To achieve this requirement we follow below methods.

  1. How to Bind XML File Data into First DropDownList 1
  2. Get DropDownList 2 values based on SelectedIndexChanged of DropDownList 1
  3. Filter Gridview Data Based on DropDownList 1, DropDownList 2 , DropDownList 3

1.) How to Bind XML File Data into First DropDownList 1

to bind xml file data, first we need to add Dropdownlist controls in design page and also add one Gridview control to Default.aspx.
Properties for DropDownList Controls:
DropDownList1 : ID = ddl_state | AutoPostBack = true | Event = Double Click on SelectedIndexChanged Event
DropDownList2 : ID = ddl_city
DropDownList3 : ID = ddl_bloodgroup
GridView1 : ID = GridView1 | AutoFormat : Select Color format


HTML Markup : Design Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>    
    <table>
     <tr>
      <td>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">        
        <ContentTemplate>        
        <table>
        <tr>
        <td>
        <asp:DropDownList ID="ddl_state" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddl_state_SelectedIndexChanged">
        </asp:DropDownList>
        </td>
        <td>
        <asp:DropDownList ID="ddl_city" runat="server">
        </asp:DropDownList>
        </td>
        </tr>
        </table>
        </ContentTemplate>
        <Triggers>
         <asp:AsyncPostBackTrigger ControlID="ddl_state" EventName="SelectedIndexChanged" />
        </Triggers>
        </asp:UpdatePanel>
        </td>
        <td>
        <asp:DropDownList ID="ddl_bloodgroup" CssClass="ddl" runat="server">
         <asp:ListItem Text="Blood Group" Value="0"></asp:ListItem>
         <asp:ListItem Text="A+ " Value="1"></asp:ListItem>
         <asp:ListItem Text="A- " Value="2"></asp:ListItem>
         <asp:ListItem Text="B+ " Value="3"></asp:ListItem>
         <asp:ListItem Text="B- " Value="4"></asp:ListItem>
         <asp:ListItem Text="AB+" Value="5"></asp:ListItem>
         <asp:ListItem Text="AB-" Value="6"></asp:ListItem>
         <asp:ListItem Text="O+ " Value="7"></asp:ListItem>
         <asp:ListItem Text="O- " Value="8"></asp:ListItem>
       </asp:DropDownList>
       </td>
       <td>
        <asp:Button ID="btn_filter" runat="server" Width="100px" Text="Filter" 
               onclick="btn_filter_Click" />
        </td>
        </tr>
        </table>    
    </div>
    <br /><br />
    <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" 
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
        CellSpacing="2">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FFF1D4" />
        <SortedAscendingHeaderStyle BackColor="#B95C30" />
        <SortedDescendingCellStyle BackColor="#F1E5CE" />
        <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>
    </form>
</body>
</html>


Bind Data From SQL Server to Gridview

To Bind SQL - to - Gridview i written a Method called private void BindFillData() Method is used to Bind Data into Gridview and we can access this method through out the class. In this Method i'm using ViewState["dd"] and assinging Dataset data into it. Now that ViewState contain(Select * from donardetails) data.


private void BindFillData()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
        SqlDataAdapter da=new SqlDataAdapter("Select * from donardetails", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ViewState["dd"] = ds.Tables[0];
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

    }

Bind XML Data into DropDownList State

DisplayState() Method is used to Read XML File Data and Bind to DropDownList(ddl_state).
First we need to Create a Object for DataSet ds = new DataSet(), in DataSet Class their is a predefined method called ReadXml Method. This method is used to read XML Data simplly passing the Path of the XML File Location to the ReadXml Method. let see programmatically



 private void DisplayState()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/XML/State-City-Language.xml"));
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddl_state.DataSource = ds;
            ddl_state.DataTextField = "Name";
            ddl_state.DataBind();
            ddl_state.Items.Insert(0, new ListItem(" Choose Your State ", "0"));
            ddl_city.Items.Insert(0, new ListItem(" Choose Your City ", "0"));
            ddl_city.Enabled = false;
            ddl_state.SelectedIndex = 0;
            ddl_city.SelectedIndex = 0;
        }
    }


Display DropDownlist City Based on DropDownlist State :



  protected void ddl_state_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_state.SelectedValue != "0")
        {
            ddl_city.Items.Clear();
            DataSet dscity = new DataSet();
            dscity.ReadXml(Server.MapPath("~/XML/State-City-Language.xml"));
            if (dscity.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dscity.Tables[0].Rows.Count; i++)
                {
                    if (ddl_state.SelectedItem.Text == dscity.Tables[0].Rows[i]["Name"].ToString())
                    {
                        string city = dscity.Tables[0].Rows[i]["City"].ToString();
                        ddl_city.Enabled = true;
                        ddl_city.Items.Insert(0, new ListItem(" Choose Your City ", "0"));
                        ddl_city.Items.Add(city);
                        break;
                    }
                }
            }
        }
        else
        {
            ddl_city.Items.Clear();
            ddl_city.Items.Insert(0, new ListItem(" Choose Your City ", "0"));
            ddl_city.Enabled = false;
        }

    }


C# Code : Filter Button Click



 protected void btn_filter_Click(object sender, EventArgs e)
    {
        string state = ddl_state.SelectedItem.Text.Trim();
        string city = ddl_city.SelectedItem.Text.Trim();
        string bloodgroup = ddl_bloodgroup.SelectedItem.Text.Trim(); ;
        DataTable dt = (DataTable)ViewState["dd"];
        if (dt.Rows.Count > 0)
        {
            DataTable dtfilter = new DataTable();
            dtfilter.Columns.Add("ID");
            dtfilter.Columns.Add("donarusername");
            dtfilter.Columns.Add("donarfullname");
            dtfilter.Columns.Add("donardob");
            dtfilter.Columns.Add("donargender");
            dtfilter.Columns.Add("donarstate");
            dtfilter.Columns.Add("donarcity");
            dtfilter.Columns.Add("donaremailid");
            dtfilter.Columns.Add("donarphone");
            dtfilter.Columns.Add("donarbloodgroup");
            DataRow drfilter = null;
            DataRow[] drw = dt.Select("donarstate='" + state + "' AND donarcity='" + city + "' AND donarbloodgroup='" + bloodgroup + "'");//            
            if (drw.Length > 0)
            {
                foreach (DataRow r in drw)
                {
                    drfilter = dtfilter.NewRow();
                    drfilter["ID"] = r["ID"].ToString();
                    drfilter["donarusername"] = r["donarusername"].ToString();
                    drfilter["donarfullname"] = r["donarfullname"].ToString();
                    drfilter["donardob"] = r["donardob"].ToString();
                    drfilter["donargender"] = r["donargender"].ToString();
                    drfilter["donarstate"] = r["donarstate"].ToString();
                    drfilter["donarcity"] = r["donarcity"].ToString();
                    drfilter["donaremailid"] = r["donaremailid"].ToString();
                    drfilter["donarphone"] = r["donarphone"].ToString();
                    drfilter["donarbloodgroup"] = r["donarbloodgroup"].ToString();
                    dtfilter.Rows.Add(drfilter);
                    GridView1.DataSource = dtfilter;
                    GridView1.DataBind();
                }
            }
            else
            {
                GridView1.EmptyDataText = " No Record Found....";
                GridView1.DataBind();
            }
        }
    }


To Download this Project Source Code Click on below link


Download Project
0 Komentar untuk "Display Particular Data in Gridview without using SQL Query ( Select * From TableName ) "

Back To Top