DataGrid.Columns(Index)

  • Thread starter Thread starter RN1
  • Start date Start date
R

RN1

I am retrieving records from a DB table & displaying them in a
DataGrid. There are 6 columns in the DB table & the DataGrid displays
all the 6 columns. This is the code:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(".....")
sqlDapter = New SqlDataAdapter("SELECT * FROM TestMarks",
sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Marks")

dgMarks.DataSource = dSet.Tables("Marks").DefaultView
dgMarks.DataBind()

dgMarks.Columns(0).ItemStyle.HorizontalAlign =
HorizontalAlign.Right
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgMarks" runat="server"/>
</form>

I want the records in the first column in the DataGrid to be right
aligned which is why I have the line

dgMarks.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right

in the above code but the above line generates the following error:

Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

Why is that line generating the above error? Also how do I make the
first column in the DataGrid right aligned?

Thanks,

Ron
 
Hi

I guess you are using auto generated columns...

try using rowdatabound event to format your columns datas...

best of luck

Munna
 
Hi

I guess you are using auto generated columns...

try using rowdatabound event to format your columns datas...

best of luck

Munna

Do you mind showing a small example?

Thanks,

Ron
 
Back
Top