2011年2月23日 星期三

Loading a GridView into a DataTable and then back to another GridView

http://techron.blogspot.com/2007/10/loading-gridview-into-datatable-and.html

First, Loading a GridView into a DataTable, dt:


private void initDataTable()
{

int i = 0;
int j = 0;
int totRow = GridView1.Rows.Count;
int totCol = GridView1.Rows[0].Cells.Count;
// Set the table headers
string[] arrHeaders = {"Symbol","Name","Exchange" };
// Get the table headers
for (j = 0; j < totCol; j++)
{
// The commented line would work if the Gridview had Column HeaderText
//dt.Columns.Add(new DataColumn(GridView1.Columns[col].HeaderText, typeof(string)));

// Instead I use the Headers array, arrHeaders
dt.Columns.Add(new DataColumn(arrHeaders[j].ToString(), typeof(string)));
}
DataRow dr;
for (i = 0; i < totRow; i++)
{
dr = this.dt.NewRow();
for (j = 0; j < totCol; j++)
{
dr[j] = GridView1.Rows[i].Cells[j].Text;
}
this.dt.Rows.Add(dr);
}
}

Then back to another GridView:


private void fillGridView2()
{
if (dt.Rows.Count > 0)
{
GridView2.DataSource = dt;
GridView2.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
GridView2.DataSource = dt;
GridView2.DataBind();

int TotalColumns = GridView2.Rows[0].Cells.Count;
GridView2.Rows[0].Cells.Clear();
GridView2.Rows[0].Cells.Add(new TableCell());
GridView2.Rows[0].Cells[0].ColumnSpan = TotalColumns;
GridView2.Rows[0].Cells[0].Text = "No Record Found";
}
}

The no record found code was from this link.
Posted by techron at 5:10 PM
Labels: C# ADO

2011年2月20日 星期日

好想走

為什麼妳不相信我
胸口很悶
如果就此心臟病發走了也好
這樣是不是對妳我都好呢
對他是不是也比較好
我情緒的不穩定對他對妳都會有很大的影響