PC Review


Reply
Thread Tools Rate Thread

DataGrid.Columns(Index)

 
 
RN1
Guest
Posts: n/a
 
      5th Aug 2008
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">
<aspataGrid 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
 
Reply With Quote
 
 
 
 
Munna
Guest
Posts: n/a
 
      5th Aug 2008
Hi

I guess you are using auto generated columns...

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

best of luck

Munna
 
Reply With Quote
 
RN1
Guest
Posts: n/a
 
      5th Aug 2008
On Aug 5, 2:48*pm, Munna <munna...@gmail.com> wrote:
> 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
 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      5th Aug 2008
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" <(E-Mail Removed)> wrote in message
news:772fecae-f665-462e-9385-(E-Mail Removed)...
>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">
> <aspataGrid 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid, binded columns and unbinded columns =?Utf-8?B?S2VycnkgTW9vcm1hbg==?= Microsoft VB .NET 4 28th Apr 2006 06:02 AM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth Microsoft Dot NET Framework 0 5th Apr 2005 04:54 AM
DataGrid Component | Displaying columns of a DataSet as Rows in a DataGrid ? Diego TERCERO Microsoft C# .NET 3 19th Dec 2003 02:21 PM
if I have a datagrid, how can i do the selectedindexchange on the selected index on that datagrid?? Jason Microsoft Dot NET Framework Forms 1 17th Oct 2003 01:46 PM
Retrieving data table index for datagrid index selected Dvae c Microsoft Dot NET Compact Framework 2 5th Oct 2003 01:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:58 AM.