Results 1 to 10 of 10

Thread: Displaying number of columns of a Datagrid...

  1. #1

    Thread Starter
    Lively Member VB_client's Avatar
    Join Date
    Feb 2004
    Location
    India
    Posts
    87

    Displaying number of columns of a Datagrid...

    I am developing a datgrid in vb.net
    I gave response.write statement to get the number of columns in the datagrid.

    VB Code:
    1. sqlConn = New OracleConnection(State.sConnection)
    2.         sqlConn.Open()
    3.  
    4.         sqlDa = New OracleDataAdapter(sqlsearchquery, sqlConn)
    5.         sqlDa.Fill(sqlDs)
    6.  
    7.         DataGrid1.DataSource = sqlDs
    8.         DataGrid1.DataBind()
    9.         Response.Write("Datagrid Columns:" & DataGrid1.Columns.Count.ToString())

    The Datagrid shows 17 columns on display.
    But the response.write returns 0 columns. WHY ?


    anybody please help on this.
    In order to be successful all you need is a little faith within yourself

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying number of columns of a Datagrid...

    Bind the Table to the Grid instead of the whole DataSet!

    VB Code:
    1. sqlConn = New OracleConnection(State.sConnection)
    2.         sqlConn.Open()
    3.  
    4.         sqlDa = New OracleDataAdapter(sqlsearchquery, sqlConn)
    5.         sqlDa.Fill(sqlDs)
    6.  
    7.         DataGrid1.DataSource = sqlDs[COLOR=DarkRed][B].Table(0)[/B][/COLOR]
    8.         DataGrid1.DataBind()
    9.         Response.Write("Datagrid Columns:" & DataGrid1.Columns.Count.ToString())
    Using VS 2010 on Fw4.0

  3. #3

    Thread Starter
    Lively Member VB_client's Avatar
    Join Date
    Feb 2004
    Location
    India
    Posts
    87

    Re: Displaying number of columns of a Datagrid...

    NO, IT IS ALSO GIVING ME 0..

    This is the code I used for this (.Net 2003)

    VB Code:
    1. Dim sqlDa As OracleDataAdapter
    2.         Dim sqlConn As OracleConnection
    3.         Dim sqlDs = New DataSet
    4.         Dim sqlQuery As String
    5.  
    6.         sqlConn = New OracleConnection(State.sConnection)
    7.         sqlConn.Open()
    8.  
    9.         sqlDa = New OracleDataAdapter(sqlsearchquery, sqlConn)
    10.         sqlDa.Fill(sqlDs)
    11.  
    12.         DataGrid1.DataSource = sqlDs.Tables(0)
    13.         DataGrid1.DataBind()
    14.         Response.Write("Datagrid Columns:" & DataGrid1.Columns.Count.ToString())
    In order to be successful all you need is a little faith within yourself

  4. #4
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying number of columns of a Datagrid...

    What type of Grid is this ???
    Cause System.Windows.Forms.DataGrid did'nt have Columns property

    Emm and what about this ?
    VB Code:
    1. Dim sqlDa As OracleDataAdapter
    2.     Dim sqlConn As OracleConnection
    3.     Dim sqlDs As New DataSet [I]' You forget the AS here (Instead of Dim sqlDs = New DataSet)[/I]
    4.     Dim sqlQuery As String
    5.  
    6.     sqlConn = New OracleConnection(State.sConnection)
    7.     sqlConn.Open()
    8.  
    9.     sqlDa = New OracleDataAdapter(sqlsearchquery, sqlConn)
    10.     sqlDa.Fill(sqlDs)
    11.  
    12.     DataGrid1.DataSource = sqlDs.Tables(0)
    13.     DataGrid1.DataBind()
    14.     Response.Write("Datagrid Columns:" & [COLOR=DarkRed][B]sqlDs.Tables(0).Columns.Count[/B][/COLOR]
    Using VS 2010 on Fw4.0

  5. #5

    Thread Starter
    Lively Member VB_client's Avatar
    Join Date
    Feb 2004
    Location
    India
    Posts
    87

    Re: Displaying number of columns of a Datagrid...

    yes, this gives me the number of columns from the dataset.

    But I want the number of columns from the Data Grid only.

    Coz, I want to set the Column Heading of the Data Grid, which is retrieved from another Dataset. Iam using the below code,

    ...after data binding....
    DataGrid1.Columns(0).HeaderText = sqlDs1.Tables(0).Rows(0).Item(0).ToString()

    This gives me error like "Index out of Range".
    In order to be successful all you need is a little faith within yourself

  6. #6
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying number of columns of a Datagrid...

    Quote Originally Posted by VB_client
    yes, this gives me the number of columns from the dataset.

    But I want the number of columns from the Data Grid only.

    Coz, I want to set the Column Heading of the Data Grid, which is retrieved from another Dataset. Iam using the below code,

    ...after data binding....
    DataGrid1.Columns(0).HeaderText = sqlDs1.Tables(0).Rows(0).Item(0).ToString()

    This gives me error like "Index out of Range".
    Is this ASP.NET ??
    It really strange cause DataGrid from FrameWork 1.1 did'nt have Columns Propetry, is this something that you add youself? or third party?

    And DataGrid Style should be done with DataGridTableStyle and DataGridColumnStyle.
    All of this is define here:
    http://msdn.microsoft.com/library/de...lumnstyle1.asp
    Using VS 2010 on Fw4.0

  7. #7

    Thread Starter
    Lively Member VB_client's Avatar
    Join Date
    Feb 2004
    Location
    India
    Posts
    87

    Re: Displaying number of columns of a Datagrid...

    Yes, Its ASP.NEt and I am using framework 1.1 and the datagrid has Column properties.
    In order to be successful all you need is a little faith within yourself

  8. #8
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying number of columns of a Datagrid...

    Since this is ASP.NET why don't you post at the ASP.NET Forum ?

    Not that i don't want to help, but Since this is the VB.NET Forum, are you expecting to receive ASP.NET answer here

    thing are a bit different in ASP.NET...

    Regard
    Using VS 2010 on Fw4.0

  9. #9
    Hyperactive Member rplcmint's Avatar
    Join Date
    Jan 2001
    Location
    Stockton, CA
    Posts
    333

    Re: Displaying number of columns of a Datagrid...

    I thought it was something like this.

    Datagrid1.items.Cells.Count or something like that.




    Update:

    Here is the proper way --

    Datagrid1.Items(0).Cells.Count()

    assuming that there are rows in the datagrid (which there should be if you populated it with data! )
    Last edited by rplcmint; Nov 14th, 2005 at 06:08 PM.

  10. #10

    Thread Starter
    Lively Member VB_client's Avatar
    Join Date
    Feb 2004
    Location
    India
    Posts
    87

    Re: Displaying number of columns of a Datagrid...

    Okay, Thanks, I'll do it rightaway!
    In order to be successful all you need is a little faith within yourself

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width