DataGrid.Columns(Index)


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
 
Ad

Advertisements

M

Munna

Hi

I guess you are using auto generated columns...

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

best of luck

Munna
 
R

RN1

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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Insert New Record 3
Text Alignment In DataGrid? 7
Show "No Records Found" Message 1
DataRow Delete Method 5
Default Selected Item in DropDownList 4
DropDownList DataGrid 1
DB Value in Label 1
DataBind 1

Top