Name |
month |
val |
|
suresh |
jan |
12000 |
|
praveen |
feb |
14000 |
|
Raja |
mar |
13000 |
OUTPUT
Name suresh Praveen Raja
Month jan feb mar --------- dec total
Val 12000 14000
13000
------ 1000
Hi:
Can any one send me the Sql Query to achieve the following task.
Write query to display row values in to column value
Kirtan PatelPosted Aug 21, 2009, 9:04 AM
Here is How i resolved it .
dont forget to mark "Do you like answer" it will give me some credit :)
take a if you dont understand code here i have also provided Project Files With database i created Download Project Files .i have provided link at bottom of the post :)
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("select * from TestTable",con);
SqlDataReader reader = comm.ExecuteReader();
List<string> Names = new List<string>();
List<string> Months = new List<string>();
List<string> Values = new List<string>();
while (reader.Read())
{
Names.Add(reader[0].ToString());
Months.Add(reader[1].ToString());
Values.Add(reader[2].ToString());
}
int length = Names.Count -1;
for (int i = 0; i <= length; i++)
{
DataGridViewColumn c = new DataGridViewColumn(new DataGridViewTextBoxCell());
if (dataGridView1.Columns.Count == 0)
{
c.HeaderText = "";
dataGridView1.Columns.Add(c);
dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[0].Value = "Names";
}
else
{
c.HeaderText = "";
dataGridView1.Columns.Add(c);
}
}
for (int i = 1; i <= length; i++)
{
if (i == 1)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = "Month";
}
else if (i==2)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = "Values";
}
}
for (int i = 0; i < length; i++)
{
dataGridView1.Rows[0].Cells[i + 1].Value = Names[i];
dataGridView1.Rows[1].Cells[i + 1].Value = Months[i];
dataGridView1.Rows[2].Cells[i + 1].Value = Values[i];
}
con.Close();
}
SreekanthPosted Aug 21, 2009, 8:54 AM