![]() String csvString = CSV_Manager.ExportDatatableToCSV(MyDt) // Pass // the datasource (datatable) // of your gridview as the parameter to convert to csv double totalSalary = MyDt.AsEnumerable().Sum(row => StringBuilder sbAgr = new StringBuilder() ĭataTable MyDt = CSV_Manager.ArbitraryDatatable() // Your datatable // can also come // from any database connection, this Arbitrary Datatable is just for a test Export data to CSV/Excel protected void Button1_Click( object sender, EventArgs e) Private void BindGrid(DataTable MyDt = null)īindDt = CSV_Manager.ArbitraryDatatable() // Your datatable // could come from any database connectionĭouble totalSalary = BindDt.AsEnumerable().Sum Protected void Page_Load( object sender, EventArgs e) CODEGEN: This method call is required by the Web Form Designer // Do not modify it using the code editor. GridView1.RowDataBound += new GridViewRowEventHandler GridView1.RowDataBound -= new GridViewRowEventHandler Public partial class ManageGridView : .Pageīutton1.Click -= new System.EventHandler( this.Button1_Click) īutton1.Click += new System.EventHandler( this.Button1_Click) In order to adapt this project to your situation, you must add reference to "Microsoft.VisualBasic" and add the following to the two classes in the web form codebehind and the other class for CSV. The entire code is hereby attached, it was written in C# and in 3.5 Framework, may also work in lower or higher frameworks. ![]() Aggregating column data ( Sum, Count, Average, etc.).Populating and formatting Gridview cells.We will address these three areas of challenge:. This project presents basically three things that usually give developers challenges and consume their valuable time using GridView or Datagrid programmatically. You can change the datatable to your own datatable coming from your database connection. As long as you have a datatable that can be the datasource of your GridView, you are good to go.įor the purpose of this project, because we do not want to establish any database connection, I just created an arbitrary datatable and manually populated it for my demo (salary stuff). This project is not specific about your database connection that will generate your datatable. So also, you want to export the data to Excel, Word or PDF. We are aware it is possible to set some properties at design mode, but at times, you do not know the complete structure of your datatable and you want to do this programmatically. Note that we are only looking at pure programming without formatting and setting fields/properties in Visual Studio design mode. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |