locked
How to clear all columns in asp.net gridview RRS feed

  • Question

  • Hello all,

    I have created columns dynamically in asp.net gridview. When i press a button to retreive data in gridview, it works ok for the first time. But when i press button for the second time additional/duplicate columns add to gridview and so on.

    My question is that how can we remove all columns and then populate these columns with fresh values ???

    Following is my code

     If Me.GridView1.Columns.Count > 1 Then
                            Me.GridView1.Columns.RemoveAt(Me.GridView1.Columns.Count - 1)
                        End If

                        VFromDate = CDate(tbFromDate.Text).AddDays(-1)
                        VToDate = CDate(tbToDate.Text).AddDays(+1)

                        Dim VFromDateCstr As String = CStr(VFromDate.ToString("yyyy-MM-dd")) '& "T20:22:23Z"
                        Dim VToDateCstr As String = CStr(VToDate.ToString("yyyy-MM-dd")) '& "T20:22:23Z"

                        Dim query As New SPQuery

                        If ddlDepartment.Text = "" Then
                            query.Query = "<Where><And><Gt><FieldRef Name='Created' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + VFromDateCstr + "</Value></Gt><Lt><FieldRef Name='Created' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + VToDateCstr + "</Value></Lt></And></Where>"
                        Else
                            query.Query = "<Where><And><Gt><FieldRef Name='Created' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + VFromDateCstr + "</Value></Gt><And><Lt><FieldRef Name='Created' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + VToDateCstr + "</Value></Lt><Eq><FieldRef Name='Case_x0020_Status' /><Value Type='Text'>" + ddlDepartment.Text + "</Value></Eq></And></And></Where>"
                        End If


                        Dim list As SPList = web.Lists("CreditCaseTracking")
                        Dim dt As New DataTable
                        dt = list.GetItems(query).GetDataTable

                        If list.GetItems(query).Count > 0 Then

                            Dim BranchName As New BoundField()
                            BranchName.DataField = "Branch_x0020_Name_x0020_Branch"
                            BranchName.HeaderText = "Branch Name"
                            GridView1.Columns.Add(BranchName)

                            Dim CoName As New BoundField()
                            CoName.DataField = "Co_x0020_Name_x0020_Branch"
                            CoName.HeaderText = "CO Name"
                            GridView1.Columns.Add(CoName)

                            GridView1.DataSource = dt
                        

                        End If

    Friday, September 14, 2012 9:37 AM

Answers

  • Thanks i done it by the following way.

    GridView1.Columns(1).ItemStyle.Width = 15000

    • Marked as answer by Rauf Ab Friday, September 14, 2012 11:45 AM
    Friday, September 14, 2012 11:45 AM
  • HI,

    Use below code to clear column after "If list.GetItems(query).Count > 0 Then" since you are binding your column everytime:

    gridview1.Columns.Clear()
    
    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Marked as answer by Rauf Ab Friday, September 14, 2012 11:44 AM
    Friday, September 14, 2012 10:05 AM
  • Hi,

    You can use below code on rowdaatbound.:

    Dim cell As TableCell = e.Row.Cells(0)
    cell.Width = New Unit("200px")

    Check this for more information:http://www.codeproject.com/Tips/301449/Set-Width-of-gridview-columns-dynamically-when-Aut

    Good luck


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Marked as answer by Rauf Ab Friday, September 14, 2012 11:44 AM
    Friday, September 14, 2012 11:16 AM

All replies

  • HI,

    Use below code to clear column after "If list.GetItems(query).Count > 0 Then" since you are binding your column everytime:

    gridview1.Columns.Clear()
    
    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Marked as answer by Rauf Ab Friday, September 14, 2012 11:44 AM
    Friday, September 14, 2012 10:05 AM
  • Ohhh thanks,

    I done it, i forgot very little thing. Please tell one more thing that how can we programatically set the width of gridview column.

    Thanks.

    Friday, September 14, 2012 10:49 AM
  • Hi,

    You can use below code on rowdaatbound.:

    Dim cell As TableCell = e.Row.Cells(0)
    cell.Width = New Unit("200px")

    Check this for more information:http://www.codeproject.com/Tips/301449/Set-Width-of-gridview-columns-dynamically-when-Aut

    Good luck


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Marked as answer by Rauf Ab Friday, September 14, 2012 11:44 AM
    Friday, September 14, 2012 11:16 AM
  • Thanks i done it by the following way.

    GridView1.Columns(1).ItemStyle.Width = 15000

    • Marked as answer by Rauf Ab Friday, September 14, 2012 11:45 AM
    Friday, September 14, 2012 11:45 AM