Vivek Kumar Vishwas

Vivek Kumar Vishwas

  • NA
  • 115
  • 56.5k

How to create a report in database table as treeview design

Nov 18 2017 1:20 AM
Hello Guys !!
 
Let's have a look on my requirmnets...
  • Creating N level of  Nested Gridview form data base table
  • How to Show data as parent child hierarchy in asp.netc#
What i have done in code let's have a look
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Report.aspx.cs" Inherits="Report" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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"
EnablePageMethods = "true">
</asp:ScriptManager>
<div>
<center>Report</center>
<fieldset style="width: 25%; float: left;">
<legend>Type Parent</legend>
<asp:TextBox ID="txttext" runat="server" Placeholder="Type Name..." Style="width: 100%;
height: 30px;"></asp:TextBox><br />
<asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="txttext"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</asp:AutoCompleteExtender>
<asp:Button ID="btn_save" runat="server" Text="Save" OnClick="btn_save_Click" />
</fieldset>
<div style="clear:both"></div>
<asp:GridView ID="GridView1" runat="server" />
</div>
</form>
</body>
</html>
 
 
 
.cs-------------------
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using MyGreatVision;
public partial class Report : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}
protected void btn_save_Click(object sender, EventArgs e)
{
String query = "Select * from TopBill where Pname='"+txttext.Text+"'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtMasterBill = new DataTable();
sda.Fill(dtMasterBill);
if (dtMasterBill.Rows.Count > 0)
{
GridView1.DataSource = dtMasterBill;
GridView1.DataBind();
string RowValue = GridView1.Rows[0].Cells[1].Text;
// do something with RowValue
con.Close();
//Response.Write("'" + RowValue + "'");
GridView gdv = new GridView();
String query1 = "Select * from TopBill where Pname='" + RowValue + "'";
SqlCommand cmd1 = new SqlCommand(query1, con);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dtMasterBill1 = new DataTable();
sda.Fill(dtMasterBill1);
if (dtMasterBill.Rows.Count > 0)
{
gdv.DataSource = dtMasterBill1;
gdv.DataBind();
Response.Write(dtMasterBill1);
}
}
else
{
// fieldReptr.Visible = false;
}
}
private void GetData()
{
String query = "Select * from TopBill where Pid=0";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtMasterBill = new DataTable();
sda.Fill(dtMasterBill);
if (dtMasterBill.Rows.Count > 0)
{
GridView1.DataSource = dtMasterBill;
GridView1.DataBind();
}
else
{
// fieldReptr.Visible = false;
}
}
//
// Auto extender.....
//--------- Auto Extender
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCompletionList(string prefixText, int count)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand com = new SqlCommand())
{
com.CommandText = "select CName from TopBill where " + "cName like @Search + '%'";
com.Parameters.AddWithValue("@Search", prefixText);
com.Connection = con;
con.Open();
List<string> countryNames = new List<string>();
using (SqlDataReader sdr = com.ExecuteReader())
{
while (sdr.Read())
{
countryNames.Add(sdr["cName"].ToString());
}
}
con.Close();
return countryNames;
}
}
}
}
 
 
i want   to show my data in the below hierarchy.
 
 
 
 
Suppose i have a table that contains Pid , Cid, Pnamem Cname, Srno.
in my table cid is repeating multiple times in pid column.

Now , i want to show all data in grid view, its showing fine. as my grid view binding process is going on in code behind i have to find pid of reach row to check that is pid has repeating more than once. if yes, at the same time i want to automatically create a grid view to show the data according to each row of pid if pid is more than one time.

let's see.

Srno Id Pname cid Cname Cqty
11 0 15 America
12 15 America 1510 New Jursy 10
13 15 America 1511 New Jersy 5

i write a code to bind the data in to grid view

sql query is like this - select cid, cname from TopBill where Pid=0

you will get the result like -
CId Cname
15 America
Now as the above data is beinding in grid view , we have to check the grid view row and find the Cid, and if CId is in PId Column we have to create grid view to bind the data according to founded ID.

This is what i need
 
find my attached  file and solve the problem urgently.
 

Attachment: Hierarchy.rar