Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir, i create a gridview at runtime with unknown number of columns. basically 2 columns are repeated more then one time. and my gridview header is show like:-


[SNo] [RoomNo] [SNo1] [RoomNo1] [SNo2] [RoomNo2]

it automatically add 1,2 and so on with repeated header name. but i need same header name look like :-

[SNo] [RoomNo] [SNo] [RoomNo] [SNo] [RoomNo]

how can i do it..
Posted
Comments
Laiju k 22-Nov-14 1:18am    
where is the code you used.post the code for more details.
TCS54321 22-Nov-14 2:11am    
Protected Sub BtnPrint2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnPrint2.Click

Dim sem As String
Dim str As String
Dim str1 As String
sql = "select distinct semid from matrix where courseid=" & DdlCourseWise.SelectedValue & ""
Dim dt As DataTable = Hari.Utility.db.GetRecodeDT(sql)
If dt.Rows.Count = 0 Then
dt.Dispose()
Amit.Messagebx.Alert(Me, "No Data Available")
Exit Sub
End If


For k = 0 To dt.Rows.Count - 1
sem += dt.Rows.Item(k)("semid").ToString + ","
Next
sem = Left(sem, sem.Length - 1)
Dim sid As String() = Split(sem, ",")

For k = 0 To dt.Rows.Count - 1
str += "R" + dt.Rows.Item(k)("semid").ToString + ".RowNumber as Sno," + "R" + dt.Rows.Item(k)("semid").ToString + ".AdmissionNo," + "R" + dt.Rows.Item(k)("semid").ToString + ".RoomNo" + ","
If str1 Is Nothing Then
str1 += "(Select RoomNo,AdmissionNo, ROW_NUMBER() OVER (ORDER BY Matrix.CourseId) 'RowNumber' FROM Matrix inner join student on student.studentid=matrix.studentid WHERE Matrix.CourseId = " & DdlCourseWise.SelectedValue & " and Matrix.SemId = " + dt.Rows.Item(k)("semid").ToString + ") AS " + "R" + dt.Rows.Item(k)("semid").ToString + ""
Else
str1 += " FULL OUTER JOIN (select RoomNo,AdmissionNo, ROW_NUMBER() OVER (ORDER BY Matrix.CourseId) 'RowNumber' FROM Matrix inner join student on student.studentid=matrix.studentid WHERE Matrix.CourseId = " & DdlCourseWise.SelectedValue & " and Matrix.SemId = " + dt.Rows.Item(k)("semid").ToString + ") AS " + "R" + dt.Rows.Item(k)("semid").ToString + "" + " ON R" & sid(0) & ".RowNumber = R" & dt.Rows.Item(k)("semid").ToString & ".RowNumber"
End If
Next

dt.Dispose()

sql = " SELECT " & _
"" & str & " " & _
" FROM " & _
" " & str1 & ""
Dim dt1 As DataTable = Hari.Utility.db.GetRecodeDT(sql)
Gridview2.DataSource = dt1
Gridview2.DataBind()
dt1.Dispose()
End Sub

1 solution

When filling datatable, if there are same column name in the datasource then it will add numbers to that.

You can change the gridview header in the Databound event

C#
protected void Gridview2_DataBound(object sender, EventArgs e)
     {

         foreach (TableCell cl in Gridview2.HeaderRow.Cells)
         {
             if (cl.Text.StartsWith("SNo"))
                 cl.Text = "SNo";

             if (cl.Text.StartsWith("RoomNo"))
                 cl.Text = "RoomNo";

         }
     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900