473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

DataGrid.Columns(Index)

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
Aug 5 '08 #1
3 1375
Hi

I guess you are using auto generated columns...

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

best of luck

Munna
Aug 5 '08 #2
RN1
On Aug 5, 2:48*pm, Munna <munna...@gmail.comwrote:
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
Aug 5 '08 #3
Autogenerated columns don't participate in the Columns property. But you can
use RowCreated event to style the columns in each row as it gets created.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"RN1" <rn**@rediffmail.comwrote in message
news:77**********************************@j7g2000p rm.googlegroups.com...
>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

Aug 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Prateek | last post by:
Hi, How can I change the width of a column in datagrid control? TIA Prateek
4
by: platho | last post by:
Hello, I bounced into the max 25 columns index limits on DB2 v7.2 on NT. Is this still so in other operating systems or in v8 ? Are there plans to change this in the future ? Any workarounds...
2
by: Andrew Chen | last post by:
How do I hide any particular DataGrid Cell from visible, or any DataColumn for the matter?
0
by: Samuel R. Neff | last post by:
I'm having a index problem with ListView SubItems. If I add multiple columns to the listview and then add items with associated subitems, the ListView displays fine. Then if I delete a column via...
24
by: Mike L | last post by:
This is for a Win form, in C# 2005. I want to load a datagrid, make some columns width 0, and then clean out the record I added. I get the error message, ""Index was out of range. Must be...
2
by: Reney | last post by:
I am using an access db with a table named "TimeLog". The table has the following columns: index {PK} - autoNumber employeeID {PK} - string dayOfWeek {PK} - date/time with short date (i.e....
3
by: Geoff Jones | last post by:
Hi Can anybody tell me how to find the column index in a DataGrid knowing only the text in the column heading? Any sample code would be most useful. Thanks in advance Geoff
5
by: Kathy | last post by:
I am trying to set a tooltip for a column cell in a data grid as documented in the MS Visual Studio 2005 documentation. I set up a test to match the example exactly including database column, data...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.