how to set gridview column width to auto

6,360 pts.
Tags:
DB2
SQL
Visual Basic .NET
Visual Studio 2008

I put together a simple vb program, or so I thought. I am accessing DB2 tables and trying to pull the data in using a DB2 reader. What I'd like is to have the columns be set to width=auto. The number of columns depends on the DB2 table. I'm getting an error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".

Where am I going wrong?

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="VB_test_code._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>SQL results from DB2</title>
</head>
<body>
    <form id="form1" runat="server">
<div>
    <asp:TextBox ID="tbxSQL" runat="server" Columns="80" Rows="9" TextMode="MultiLine"></asp:TextBox>
    <br />
 
</div>

    <div>
        <asp:Label ID="lblError" runat="server"></asp:Label>
<br />
 
</div>

<div>
<asp:Button ID="btnRunSQL" runat="server" Text="Run SQL Script" />
<br />
 
</div>
     
<div>
<asp:GridView ID="dgResults" AutoGenerateColumns="true" runat="server">
</asp:GridView>
</div>
    
    </form>
</body>
</html>


Imports System
Imports System.Data
Imports System.IO
Imports IBM.Data.DB2.iSeries

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Public mySQL As String = " "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            lblError.Visible = False
        End If

    End Sub

    Private Sub btnRunSQL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRunSQL.Click

        Dim mySQL As String = Request.Form("tbxSQL")
        Dim strConnection As String = "DataSource=10.10.10.11;userid=LIVEXML;password=LIVE1;DefaultCollection=MYDATA031;"

        Dim myConnection As New iDB2Connection(strConnection)
        Dim myCommand As New iDB2Command(mySQL, myConnection)
        myCommand.Connection.Open()

        Dim myReader As iDB2DataReader

        Try
            myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

            If myReader.HasRows = False Then
                lblError.Text = "No rows returned for SQL statement"
                lblError.Visible = True
            Else

                Dim fields As Integer = myReader.FieldCount

                dgResults.DataSource = myReader
                dgResults.DataBind()

                ''''''Fix columns
                For colnum As Integer = 0 To fields - 1
                    dgResults.Columns(colnum).ItemStyle.Width = "auto"
                Next
                '''''' end fix 


                myReader.Close()
                myConnection.Close()

                lblError.Text = " "
                lblError.Visible = False

            End If

        Catch ex As Exception
            lblError.Text = ex.Message
            lblError.Visible = True
        End Try

    End Sub

End Class


Software/Hardware used:
VS2008
ASKED: December 27, 2013  2:04 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following