how to rearrange column in gridview? RRS feed

  • Question

  • i retrieve gridview from database. but i change the structure of gridview. it different display from database which is i put the row in database become column in gridview. but the column i want for example Date|A|B|C|D but i get in column gridview like this Date|B|D|A|C|. the B|D|A|C is a row i retrieve from column prod_line in database. how to rearrange it back ? this is my code :
    protected void Page_Load(object sender, EventArgs e)
        //where request_date >= DATEADD(day,-8, GETDATE())
        DataTable dtTemp = new DataTable();
        cmd = new SqlCommand("SELECT request_date,prod_line,jo_no,qty,CONVERT(VARCHAR(10),need_by_date ,101) as need_by_date FROM CutPanelCard order by request_date", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        ViewState["Information"] = dtTemp;
                //DataTable dtTemp = (DataTable)ViewState["Information"];
                DataTable dtDistinctRecords = dtTemp.DefaultView.ToTable(true, "prod_line");
                DataTable dtStudentName = dtTemp.DefaultView.ToTable(true, "request_date");
                DataTable a = new DataTable();
                DataTable dtStudent = new DataTable();
                foreach (DataRow rows in dtDistinctRecords.Rows)
                foreach (DataRow row in dtStudentName.Rows)
                    DataRow dr = dtStudent.NewRow();
                    dr["request_date"] = row["request_date"];
                    DataView dv = new DataView(dtTemp);
                    dv.RowFilter = "request_date='" + row["request_date"] + "'";
                    DataTable dtStudentdtl = dv.ToTable();
                    for (int i = 0; i < dtStudentdtl.Rows.Count; i++)
                        string colValue = dtStudentdtl.Rows[i]["jo_no"].ToString();
                        string colValue2 = dtStudentdtl.Rows[i]["qty"].ToString();
                        string colValue3 = dtStudentdtl.Rows[i]["need_by_date"].ToString();
                        dr[dtStudentdtl.Rows[i]["prod_line"].ToString()] = "JO: " + colValue + "  Quantity: " + colValue2 + "  Need by Date: " + colValue3 ;
                    dtStudent.Rows.InsertAt(dr, dtStudent.Rows.Count);
                GridView1.DataSource = dtStudent;
        catch (Exception ex)

    • Moved by Sabah ShariqMVP Friday, May 12, 2017 12:03 PM Moved From Visual C#
    Friday, May 12, 2017 12:40 AM

All replies

  • Hi atinn,

    GridView() is in ASP.Net. This is Visual C# forum. As your issue is related to Web, please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off-topic.

    Your understanding and cooperation will be grateful.

    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, May 12, 2017 12:03 PM