i got a problem with开发者_Python百科 grid view ,because I'm new to asp.net(10 days),so i donno how to do it,so can you guyz please help me out this problem..
i have a simple gridview ,and i have to make new grid with row value as column header name
Empname Earnngs Amount
Austin HRA 20,000$
Austin DA 1,000$
Austin Basic 5,000$
and i have to make like this
.Austin HRA DA BASIC
2000$ 2000$ 2000$
Please give some idea and some examples using C# to do it
Here is an example of doing this using an asp GridView:
// Create new DataTable.
DataTable dt = new DataTable();
// Total count of columns.
int colCount = 3;
// Add 3 columns.
for (int i = 0; i < colCount; i++)
{
dt.Columns.Add(new DataColumn("col" + i.ToString()));
}
// Add data to the datatable.
dt.Rows.Add(new object[] { "Empname", "Earnngs", "Amount" });
dt.Rows.Add(new object[] { "Austin", "Earnngs", "Amount" });
dt.Rows.Add(new object[] { "Austin", "df", "Amount" });
dt.Rows.Add(new object[] { "sdfsdf", "dsfdf", "df" });
dt.Rows.Add(new object[] { "Empdsfsdfname", "Earnngs", "df" });
// Loop through each column in the DataTable and set the column name to the data in the first row of data.
foreach (DataColumn dc in dt.Columns)
{
dc.ColumnName = dt.Rows[0][dc].ToString();
}
// Set the datasource of the grid.
this.GridView1.DataSource = dt;
// Bind the data to the grid.
this.GridView1.DataBind();
See these:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.aspx
Strange. However create DataTable with first row values as dataColumn name and rest data will go as data row. Bind this table to the grid.
if any one face this kind of problem you can refer this coding
SqlCommand cmd = new SqlCommand("Your query", con);
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
DataRow newRow = dt.NewRow();
DataColumn dc = new DataColumn();
rdr.Read();
dt.Columns.Add(new DataColumn(rdr[Index].ToString()));
rdr.Close();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dt.Columns.Add(new DataColumn(rdr[Index].ToString()));
}
rdr.Close();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string Amount = rdr[Index].ToString();
string EarnName = rdr[Index].ToString();
newRow[EarnName] = Amount;
}
dt.Rows.Add(newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
DownLoad Torrent at High Speed
精彩评论