2012年12月18日 星期二

動態增加 GridView 資料欄 ASP.Net C #


動態增加 GridView 資料欄 ASP.Net C #

//Add GridView Column Dynamically ASP.Net and C #
  1. using System; 
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. public partial class dhilip : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.          
  13.         //實作 datatable  
  14.         DataTable dt = new DataTable();  
  15.         //實作 datarow  
  16.         DataRow drow;  
  17.         //建遒 datacolums Column1 and Column2   
  18.         DataColumn dcol1 = new DataColumn("Column1"typeof(string));  
  19.         DataColumn dcol2 = new DataColumn("Column2"typeof(string));  
  20.         //adding datacolumn to datatable  
  21.         dt.Columns.Add(dcol1);  
  22.         dt.Columns.Add(dcol2);  
  23.         //loop for 10 rows  
  24.         for (int i = 0; i < 10; i++)  
  25.         {  
  26.             //實作 datarow  
  27.             drow = dt.NewRow();  
  28.             //增加資料列到 datatable  
  29.             dt.Rows.Add(drow);  
  30.             //增加值 
  31.             dt.Rows[i][dcol1] = i.ToString();  
  32.             dt.Rows[i][dcol2] = i.ToString();  
  33.         }  
  34.         //set gridView Datasource as dataTable dt.  
  35.         GridView1.DataSource = dt;  
  36.         //Bind Datasource to gridview  
  37.         GridView1.DataBind();  
  38.     }  
  39.     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
  40.     {  
  41.         //實作 table cell  
  42.         // this table cell is going to add gridview as new column  
  43.         TableCell tc = new TableCell();  
  44.         //first cell must be -1 ie the header of gridview. if it header  
  45.         if (e.Row.RowIndex == -1)  
  46.         {  
  47.             //add header column name. you can add more styles in this section  
  48.             tc.Text = "Multiple";  
  49.             //add style for header column  
  50.             tc.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");  
  51.         }  
  52.         //cell add to gridview row  
  53.         e.Row.Cells.Add(tc);  
  54.     }  
  55.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  56.     {  
  57.         //if header column we dont need multiplication  
  58.         if (e.Row.RowIndex != -1)  
  59.         {  
  60.             //taking values from first cell. my first cell contain value, you can change  
  61.             string id = e.Row.Cells[0].Text;  
  62.             //taking values from second cell. my second cell contain value, you can change  
  63.             string id2 = e.Row.Cells[1].Text;  
  64.             //multiplication  
  65.             double mult = int.Parse(id) * int.Parse(id2);  
  66.             //adding result to last column. coz we add new column in last.  
  67.             e.Row.Cells[e.Row.Cells.Count - 1].Text = mult.ToString();  
  68.         }  
  69.     }  
  70. }  

沒有留言: