Showing posts with label gridview. Show all posts
Showing posts with label gridview. Show all posts

Thursday, 28 January 2016

printing all pages with paging in gridview in asp.net using c#

In Print button Click,

Add the following Code ,Here I want to Print GridView1 Data without Paging but  visually Paged Grid.
So Here First Remove Paging and Bind Grid then apply Javascript  for Print Preview PopUp then again Apply Paging in Gridview.


 PrintAllPages(object sender, EventArgs e)  
  {  
   GridView1.AllowPaging = false;  
   GridView1.DataBind();  
   StringWriter sw = new StringWriter();  
   HtmlTextWriter hw = new HtmlTextWriter(sw);  
   GridView1.RenderControl(hw);  
   string gridHTML = sw.ToString().Replace("\"", "'")  
    .Replace(System.Environment.NewLine, "");  
   StringBuilder sb = new StringBuilder();  
   sb.Append("<script type = 'text/javascript'>");  
   sb.Append("window.onload = new function(){");  
   sb.Append("var printWin = window.open('', '', 'left=0");  
   sb.Append(",top=0,width=1000,height=600,status=0');");  
   sb.Append("printWin.document.write(\"");  
   sb.Append(gridHTML);  
   sb.Append("\");");  
   sb.Append("printWin.document.close();");  
   sb.Append("printWin.focus();");  
   sb.Append("printWin.print();");  
   sb.Append("printWin.close();};");  
   sb.Append("</script>");  
   ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());  
   GridView1.AllowPaging = true;  
   GridView1.DataBind();  
  }   

Thursday, 26 November 2015

Adding Two Data Table using Uniqe Ids Join

  DataTable dt1 = new DataTable("Table1");  
     DataTable dt2 = new DataTable("Table2");  
     DataSet ds = new DataSet("DataSet");  
     dt1.Columns.Add("Eno", typeof(Int32));  
     dt1.Columns.Add("Ename", typeof(String));  
     dt1.Columns.Add("Salary", typeof(Double));  
     dt1.Columns.Add("Deptno", typeof(Int32));  
     dt1.PrimaryKey = new DataColumn[] { dt1.Columns["Eno"] };  
     dt2.Columns.Add("Deptno", typeof(Int32));  
     dt2.Columns.Add("Dname", typeof(String));  
     dt2.PrimaryKey = new DataColumn[] { dt2.Columns["Deptno"] };  
     ds.Tables.Add(dt1);  
     ds.Tables.Add(dt2);  
     // Loading data into dt1, dt2:  
     object[] o1 = { 1, "Ravi", 50000.50, 10 };  
     object[] o2 = { 2, "Raj", 4000.50, 20 };  
     object[] o3 = { 3, "Joni", 10000.50, 10 };  
     object[] c1 = { 10, "MFG" };  
     object[] c2 = { 20, "EAS" };  
     object[] c3 = { 30, "E&U" };  
     object[] c4 = { 40, "PES" };  
     dt2.Rows.Add(c1);  
     dt2.Rows.Add(c2);  
     dt2.Rows.Add(c3);  
     dt2.Rows.Add(c4);  
     dt1.Rows.Add(o1);  
     dt1.Rows.Add(o2);  
     dt1.Rows.Add(o3);  
     DataRelation drel = new DataRelation("EquiJoin", dt2.Columns["Deptno"], dt1.Columns["Deptno"]);  
     ds.Relations.Add(drel);  
     DataTable jt = new DataTable("Joinedtable");  
     jt.Columns.Add("Eno", typeof(Int32));  
     jt.Columns.Add("Ename", typeof(String));  
     jt.Columns.Add("Salary", typeof(Double));  
     jt.Columns.Add("Deptno", typeof(Int32));  
     jt.Columns.Add("Dname", typeof(String));  
     ds.Tables.Add(jt);  
     foreach (DataRow dr in ds.Tables["Table1"].Rows)  
     {  
       DataRow parent = dr.GetParentRow("EquiJoin");  
       DataRow current = jt.NewRow();  
       // Just add all the columns' data in "dr" to the New table.  
       for (int i = 0; i < ds.Tables["Table1"].Columns.Count; i++)  
       {  
         current[i] = dr[i];  
       }  
       // Add the column that is not present in the child, which is present in the parent.  
       current["Dname"] = parent["Dname"];  
       jt.Rows.Add(current);  
     }  
     GridView1.Visible = true;  

finally bind gridiew
      
  GridView1.DataSource = ds.Tables["Joinedtable"];  
  GridView1.DataBind();  

Tuesday, 8 September 2015

How we Get the Data Table Unique IDs Rows into another Data Table in c# for ASP.net for Grid View Binding:

  string[] FinalIDs = { };  
  DataTable dt1 = new DataTable();  
  DataTable dt2 = new DataTable();  
  FinalIDs = IDs.Distinct().ToArray();  
           int i = 0;  
       foreach (string Item in FinalIDs)  
       {  
         if (i == 0)  
         {  
           dt1.DefaultView.RowFilter = "Someid='" + Item + "'";  
           dt2 = (dt1.DefaultView).ToTable();  
         }  
         else  
         {  
           dt1.DefaultView.RowFilter = "Someid='" + Item + "'";  
           dt2.ImportRow((dt1.DefaultView).ToTable().Rows[0]);  
         }  
         i++;  
          }  
       i = 0;  

finally you can bind dt2 data table to any grid directly..

 gridView2.DataSource = dt2;  
 gridView2.DataBind();