troubleshooting Question

ASP.NET 2.0 Problem with column name changing in a Gridview control

Avatar of DeMyu
DeMyu asked on
.NET ProgrammingASP.NETVisual Basic.NET
34 Comments1 Solution566 ViewsLast Modified:
I have a gridview with AutoGenerateColumns=true that is presenting a challenge to me.

Challenge #1

The data structure from the database server follows:
Product |Qty |Jan-09|Qty|Feb-09|Qty|Mar-09 etc
-----------------------------------------------------------------
CD|1.00 |$100.00|5.00|$500.00|7.00|$700.00 etc
DVD|2.00 |$400.00|7.00|$700.00|8.00|$800.00 etc

****** But the gridview control renders the Qty columns differently, I am getting the following columns from my gridview control.

Product |Qty |Jan-09|Qty1|Feb-09|Qty2|Mar-09 etc
-----------------------------------------------------------------
CD|1.00 |$100.00|5.00|$500.00|7.00|$700.00 etc
DVD|2.00 |$400.00|7.00|$700.00|8.00|$800.00 etc

I will like to have the column names "Qty" remain as "Qty" as generated by SQL

Challenge #2
How can I ensure that the Qty column is not rendered as type money in my RowDataBound event which I am using to calculate the totals?

It is important that I keep AutoGenerateColumns=true so the gridview control can change dynamically based on the different SQL queries generated by a stored procedure.
<asp:GridView ID="ResultGridView" runat="server" 
             AutoGenerateColumns="True"
             width="100%"            
             ShowFooter="true"              
             >
              <AlternatingRowStyle CssClass="AlternatingRowStyle" />
              <RowStyle CssClass="RowStyle" />
              <HeaderStyle CssClass="gridHeader" />              
              <EmptyDataRowStyle BackColor="LightBlue" ForeColor="Red" />
             
             </asp:GridView>
 
----- code behind
 
Protected Sub ResultGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles ResultGridView.RowDataBound
        '
        ' adjust first column
        '
        Dim cell As TableCell = e.Row.Cells(1)
        Dim cell2 As TableCell = e.Row.Cells(2)
 
        If e.Row.RowType = DataControlRowType.DataRow Then
            cell.HorizontalAlign = HorizontalAlign.Left
            cell.Width = New Unit("200px")
            'ElseIf e.Row.RowType = DataControlRowType.Footer Then
            'cell.HorizontalAlign = HorizontalAlign.right
            'cell.Text = "TOTALS:"
            'cell.Font.Bold = True
        End If
 
        If e.Row.RowType = DataControlRowType.DataRow Then
            cell2.HorizontalAlign = HorizontalAlign.Left
            cell2.Width = New Unit("90px")
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            cell2.HorizontalAlign = HorizontalAlign.right
            cell2.Text = "TOTALS:"
            cell2.Font.Bold = True
        End If
 
 
        '
        ' adjust rest columns
        '
        Dim table As DataTable = DirectCast(ResultGridView.DataSource, DataTable)
        For i As Integer = 3 To e.Row.Cells.Count - 1
            Dim column As DataColumn = table.Columns(i)
            cell = e.Row.Cells(i)
            If e.Row.RowType = DataControlRowType.DataRow Then
                ' item
                Dim row As DataRow = (DirectCast(e.Row.DataItem, DataRowView)).Row
                Dim value As Decimal = DirectCast(row(i), Decimal)
                _totals(i) += value
                  cell.Text = value.ToString("c")
                        ElseIf e.Row.RowType = DataControlRowType.Footer Then
                ' footer
                cell.BackColor = System.Drawing.Color.Silver
                cell.Font.Bold = True
                cell.Text = _totals(i).ToString("c")
            End If
        Next
 
    End Sub
ASKER CERTIFIED SOLUTION
Omego2K

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 34 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 34 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros