Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid or Datagrid inside datalist/repeater - Part 2 1

Status
Not open for further replies.

DotNetGnat

Programmer
Joined
Mar 10, 2005
Messages
5,548
Location
IN
This thread is the continuation of the thread thread855-1172263.
ca8msm, everything works fine. I have just added the ItemDataBound Sub and did my formatting there...

only one aspect of my formatting is not working...

when i had the datagrid by itself( i mean not as the child grid inside the repeater)...i had the following code...

Code:
   Dim row As TableRow
        Dim i As Integer = 0

        Do While i <= [red]MyDataset.Tables("Child")[/red].Rows.Count - 2
            If MyDataset.Tables("Child").Rows(i).Item("calcLength") Is DBNull.Value Then
                Dim shRow As DataRow = MyDataset.Tables("Child").NewRow
                shRow("Station") = "11111"
                MyDataset.Tables("Child").Rows.InsertAt(shRow, i + 1)
                i = i + 1
            Else
                Dim eRow As DataRow = MyDataset.Tables("Child").NewRow
                eRow("calclength") = MyDataset.Tables("Child").Rows(i).Item("calcLength")
                eRow("calcAve_Width") = MyDataset.Tables("Child").Rows(i).Item("calcAve_Width")
                eRow("calcSF") = MyDataset.Tables("Child").Rows(i).Item("calcSF")

                MyDataset.Tables("Child").Rows.InsertAt(eRow, i + 1)
                i = i + 1
            End If
            i = i + 1
        Loop

the red part is what i tried to mimic...it was actually...ds.Tables(0).Rows

and based on this i do some more formatting in the ItemDataBound...but now this doesnt work...it just skips this formatting...

how and where can i put the above code in the example you provided...

thanks so much

-DNG
 
What we did in the last post was get get a list of all child records (MyDataset.Tables("Child")) but instead of binding each child datagrid to this table (as this would put every child record in each row of the datagrid) we called GetDataSource which in turn returned a DataView. We then used that DataView to bind each DataGrid (and therefore only show the relevant child records).

What you are doing in your example above, is checking the full child table each time and looping through that when, in fact, you should be looping through the DataView that is being used as the DataSource.

You'll either have to get a reference to the DataView or recreate it (although I'm sure the first should be possible) and loop through that instead of MyDataset.Tables("Child").


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
thanks for the explanation and it makes sense...now i have to loop through the dataview for each of the item...got that straight...

so do i do this inside the below function before returning the view
Code:
Public Function GetDataSource(ByVal dataItem As Object) As DataView
        Dim intNo As String = DataBinder.Eval(dataItem, "Item_No")
        Dim dv As DataView = MyDataset.Tables("Child").DefaultView
        dv.RowFilter = "Item_No='" & intNo & "'"
[red]'formatting/looping code here[/red]
        Return dv
    End Function

so at the red part should i have something like this:

dv.Table.Rows(i)...

thanks

-DNG

 
No, you don't. You still return the DataView as normal but you loop through the DataView instead of the child table that you have highlighted in red in your first post. As I said previously though, you'll have to figure out how to get a reference to it.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
i think i converted the code to loop through the dataview...check it out...but not sure where to put this in the whole code...not sure how to reference...

Code:
For Each dr As DataRowView In dv
            If dr.Item("calcLength") Is DBNull.Value Then
                Dim shRow As DataRow = dv.Table.NewRow
                shRow("Station") = "11111"
                dv.Table.Rows.InsertAt(shRow, i + 1)
                i = i + 1
            Else
                Dim eRow As DataRow = dv.Table.NewRow
                eRow("calcLength") = dv.Table.Rows(i).Item("calcLength")
                eRow("calcAve_Width") = dv.Table.Rows(i).Item("calcAve_Width")
                eRow("calcSF") = dv.Table.Rows(i).Item("calcSF")
            End If
        Next

-DNG
 
oops...typo..this one
Code:
        For Each dr As DataRowView In dv
            Do While i <= dv.Table.Rows.Count - 2
                If dr.Item("calcLength") Is DBNull.Value Then
                    Dim shRow As DataRow = dv.Table.NewRow
                    shRow("Station") = "11111"
                    dv.Table.Rows.InsertAt(shRow, i + 1)
                    i = i + 1
                Else
                    Dim eRow As DataRow = dv.Table.NewRow
                    eRow("calcLength") = dv.Table.Rows(i).Item("calcLength")
                    eRow("calcAve_Width") = dv.Table.Rows(i).Item("calcAve_Width")
                    eRow("calcSF") = dv.Table.Rows(i).Item("calcSF")
                End If
                i = i + 1
            Loop
        Next

-DNG
 
You put it in the sub that is called when the ItemDataBound event fires but rememeber that you can't just loop through "dv" as you have to get a reference to the specific DataView that has been created for that particular Item (hence my two comments above about getting a reference to it!)


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
to reference the dataview created for the particular item...do u think i can use the FindControl thing??

not sure when and how to reference the dataview...i also tried putting the dataview inside a session...

any suggestions...

-DNG
 
You can't use FindControl as, suprisingly, that is used to find control and your DataView isn't a control.

If you look at the DataSource property of a DataGrid you'll notice that it can either be used to Get or Set it so you can probably retrieve it from there.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
i checked the view source and this is how the each child grid's id is

Parent__ctl0_Child
Parent__ctl1_Child
Parent__ctl2_Child

do you think that pattern might helps us in any way...i did not understand what you were referring to when you said to use the get and set properties of the datasource??

Thanks so much for your guidance...

-DNG

 
ca8msm, i cudnt find much luck from all my trials...if you get some time this weekend can you put up some pseudo code for this situation...i would really appreciate that...

thanks in advance...

-DNG
 
I don't really know what you are trying to do in your first example. Is that what you had in your ItemDataBound sub (and if so, why as that means you would loop through your table everytime an item was bound).

If you want to do conditional formatting, you just have to call the sub as I said above e.g.
Code:
<asp:DataGrid onItemDataBound="Child_ItemDataBound" ...
Then, create that sub and add your conditions e.g.
Code:
    [COLOR=blue]Public[/color] [COLOR=blue]Sub[/color] Child_ItemDataBound([COLOR=blue]ByVal[/color] sender [COLOR=blue]As[/color] [COLOR=blue]Object[/color], [COLOR=blue]ByVal[/color] e [COLOR=blue]As[/color] System.Web.UI.WebControls.DataGridItemEventArgs)
        [COLOR=blue]If[/color] e.Item.ItemType = ListItemType.AlternatingItem [COLOR=blue]Or[/color] e.Item.ItemType = ListItemType.Item [COLOR=blue]Then[/color]
            [COLOR=blue]If[/color] [COLOR=blue]CInt[/color](e.Item.Cells(0).Text) > 200 [COLOR=blue]Then[/color] e.Item.BackColor = Color.Red
        [COLOR=blue]End[/color] [COLOR=blue]If[/color]
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

lets forget about the repeater for a sec...

ok now going back to the datagrid...i designed it something like...i had so much formatting to get the desired display i wanted...

i had to add some rows...hide some cells...color some cells...and so on...

from the online tutorials...i learnt that if we need/insert rows to the datagrid we should do it before binding the data to the datagrid...

so what i had was...something like this...i am not in my office right now to provide you the actual code...but here is the pseudo code...

Code:
sub page_load
'bindgrid()
end sub
sub bindgrid()
'connection string
'sql
'fill the data...
'formatting adding/inserting rows codes...etc..
mygrid.dataBind()
end sub

sub ItemDataBound()

Select case ListItem.Itemtype=ListItem.Item or ListItem.Itemtype=ListItem.AlternatingItem 
if shRow("Station") = "11111" then
' do some other kind of formatting for this newly added row...
else
'do different formatting..
endif

end sub


ok now back to the datagrid inside repeater code...everything work fine...expect adding/inserting additional rows...because the code i posted in the first post is not working as we cannot reference the Dataview and the id's of the datagrid created are dynamic...

i will post the actual code soon..

thanks

-DNG
 
I don't need to see the actual code as you've posted it at the top of this page. What I was saying was that I didn't understand why you would do conditional formatting of a DataGrid in the ItemDataBound event by looping through a DataTable as this would mean you would loop through the table everytime an item was bound.

If you want to add rows to the original datatable, you'll have to do that in the BindGrid method as a DataView is simply a view of this table.

we cannot reference the Dataview and the id's of the datagrid created are dynamic
As for this part, that's simply wrong. You can access the DataView exactly as I said above e.g.
ca8msm said:
If you look at the DataSource property of a DataGrid you'll notice that it can either be used to Get or Set it so you can probably retrieve it from there.
So you can simply get a reference to this from the ItemDataBound event by using the sender object.



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm said:
If you want to add rows to the original datatable, you'll have to do that in the BindGrid method as a DataView is simply a view of this table.

yes i did that...may be i was putting it in the wrong words...

here is the skeleton of actual code of datagrid without any repeater...

Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            BindData()
        End If

    End Sub

Sub BindData()

        Dim strConnection As String
        strConnection = "my connstring"
        Dim objConnection As OleDbConnection
        objConnection = New OleDbConnection(strConnection)
        objConnection.Open()

        Dim strSQL As String = "my sql here"

        Dim objDa As OleDbDataAdapter
        objDa = New OleDbDataAdapter(strSQL, objConnection)

        Dim ds As New DataSet
        objDa.Fill(ds)
[red]
'adding break message and other rows depending on the condition
        Dim row As TableRow
        Dim i As Integer = 0

        Do While i <= ds.Tables(0).Rows.Count - 2
            If ds.Tables(0).Rows(i).Item("calcLength") Is DBNull.Value Then
                If ds.Tables(0).Rows(i).Item("Project_ID") = ds.Tables(0).Rows(i + 1).Item("Project_ID") Then
                    Dim shRow As DataRow = ds.Tables(0).NewRow
                    shRow("Station") = "11111"
                    ds.Tables(0).Rows.InsertAt(shRow, i + 1)
                    i = i + 1
            Else
                Dim eRow As DataRow = ds.Tables(0).NewRow
                eRow("calclength") = ds.Tables(0).Rows(i).Item("calcLength")
                eRow("calcAve_Width") = ds.Tables(0).Rows(i).Item("calcAve_Width")
                eRow("calcSF") = ds.Tables(0).Rows(i).Item("calcSF")

                ds.Tables(0).Rows.InsertAt(eRow, i + 1)
                i = i + 1
            End If
            i = i + 1
        Loop

       'End inserting rows

        mydg.DataSource = ds
        mydg.DataBind()
    End Sub
[/red]
 Private Sub mydg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles mydg.ItemDataBound

 Select Case e.Item.ItemType
            Case ListItemType.AlternatingItem, ListItemType.Item
    [red]If e.Item.Cells(0).Text.Equals("11111") Then [/red]
     'doing some formatting here for that row
end if
End Select
 End sub

as you can see above the red parts...i have added rows in the BindData() sub and formatting that row in the itemdatabound()...

ca8msm said:
If you look at the DataSource property of a DataGrid...

where can i look at this...any link so that i can read and try it...

Thanks so much

-DNG
 
ok i think i got the link at msdn...and i am looking at this:

Code:
Public Sub SetDataBinding( _
   ByVal dataSource As Object, _
   ByVal dataMember As String _
)

not sure how to use it yet...

any suggestions...

-DNG
 
here is the other method i came across...

Code:
Private Function GetDataViewFromDataSource() As DataView
    ' Create a DataTable variable, and set it to the DataSource.
    Dim myDatatView As DataView
    myDatatView = CType(dataGrid1.DataSource, DataView)
    Return myDatatView
End Function 'GetDataViewFromDataSource

now in our case what would i replace datagrid1 as and where would i add/insert rows...

sorry for the lot of confusion...i have been on this one for past 3 days and mixed up with lot of thoughts...

thanks

-DNG
 
ca8msm,

i starting a new thread so that i can appraise your assistance correctly...thanks so much...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top