Home All Groups Group Topic Archive Search About

iterate through only one column of a datagrid

Author
18 Mar 2005 5:36 PM
Dave Bailey via DotNetMonster.com
I need to access the information in only one column of a datagrid.  The
column is column[3].  When iterating through the grid I want to find all
instances where the entry is "" and change it to read "N".  If the entry !+
"" then I want the entry to read "Y".  Any ideas on how I can accomplish
this task?

Thanks,

Dave

--
Message posted via http://www.dotnetmonster.com

Author
18 Mar 2005 7:10 PM
Ken Cox [Microsoft MVP]
Hi Dave,

I think it would be far easier to use a template column and work with its
data. Just jam in a good ol' IIF() and output the N or Y as the text value
depending on the evaluation. I've pasted in some demo code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

        <form id="Form1" method="post" runat="server">
            <asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
                <columns>
                    <asp:templatecolumn>
                        <itemtemplate>
                            <asp:Label runat="server" Text='<%#
iif(DataBinder.Eval(Container, "DataItem.AString")="","N","Y") %>'>
                            </asp:label>
                        </itemtemplate>
                    </asp:templatecolumn>
                </columns>
            </asp:datagrid>
        </form>

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            DataGrid1.DataSource = CreateDataSource()
            DataGrid1.DataBind()
        End If
    End Sub

    Function CreateDataSource() As DataTable
        Dim dt As New DataTable
        Dim dr As DataRow
        dt.Columns.Add(New DataColumn _
        ("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn _
        ("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn _
        ("CurrencyValue", GetType(Double)))
        dt.Columns.Add(New DataColumn _
        ("AString", GetType(String)))
        Dim i As Integer
        For i = 0 To 8
            dr = dt.NewRow()
            dr(0) = i
            dr(1) = "Item " + i.ToString()
            dr(2) = 1.23 * (i + 1)
            If (i Mod 2) = 0 Then
                dr(3) = ""
            Else
                dr(3) = "Yup"
            End If
            dt.Rows.Add(dr)
        Next i
        Return dt
    End Function 'CreateDataSource


Show quoteHide quote
"Dave Bailey via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:a93a7a74d18b485ba6c9c0819c1dcb0d@DotNetMonster.com...
>I need to access the information in only one column of a datagrid.  The
> column is column[3].  When iterating through the grid I want to find all
> instances where the entry is "" and change it to read "N".  If the entry
> !+
> "" then I want the entry to read "Y".  Any ideas on how I can accomplish
> this task?
>
> Thanks,
>
> Dave
>
> --
> Message posted via http://www.dotnetmonster.com
Author
18 Mar 2005 7:16 PM
Dave Bailey via DotNetMonster.com
Thanks,

I'll give this a try.  Is it not possible to iterate through a bound column?

--
Message posted via http://www.dotnetmonster.com
Author
18 Mar 2005 7:28 PM
Ken Cox [Microsoft MVP]
Why would you want to do that when you can make the change as the row is
being built?

Show quoteHide quote
"Dave Bailey via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:7e87ed8aac4a440497f1210157f74d2f@DotNetMonster.com...
> Thanks,
>
> I'll give this a try.  Is it not possible to iterate through a bound
> column?
>
Author
19 Mar 2005 11:33 PM
Elton Wang
Hi Dave,

Apparently you are a C# guy. You can use what Ken
suggested, template column and
condition ? truePart : flasePart
which is similar to VB IIF(condition, truePart, falsePart)
method. That is simple and only code in HTML.

Or if you still want to use bound column, then you need 
process data in ItemDataBound event:

e.Item.Cells[3].Text =  e.Item.Cells[3].Text.Equals
("") ? "N" : "Y";

HTH


Elton Wang
elton_w***@hotmail.com


>-----Original Message-----
>Thanks,
>
>I'll give this a try.  Is it not possible to iterate
through a bound column?
Show quoteHide quote
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>