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 Chriss Miller 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 3 1

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
ca8msm,

First of all thanks so much for helping me out on this one...

Here is the code i have so far...

aspx
Code:
<form id="Form1" method="post" runat="server">
			<asp:repeater id="titleRepeater" runat="server" EnableViewState="False">
				<ItemTemplate>
					<b><%# DataBinder.Eval(Container.DataItem,"Item_No") %><br>
					</b>
					<asp:DataGrid ID="maingrid" Runat="server" AutoGenerateColumns=True DataSource='<%# GetDataSource( Container.DataItem )%>' OnItemDataBound="maingrid_showformatting" ShowFooter=True>
					</asp:DataGrid>
					<br>
				</ItemTemplate>
			</asp:repeater></form>

code behind

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

Private Sub BindGrid()
        Dim strConnection As String
        strConnection = "my conn string"
        Dim objConnection As OleDbConnection
        objConnection = New OleDbConnection(strConnection)
        objConnection.Open()
        Dim MyDataRow As DataRow
        Dim StrSQL As String = "my sql1"

        Dim strSQL1 As String = "my sql 2"

        Dim objDa As OleDbDataAdapter
        Dim objDa1 As OleDbDataAdapter
        objDa = New OleDbDataAdapter(StrSQL, objConnection)
        objDa1 = New OleDbDataAdapter(StrSQL1, objConnection)

        objDa.Fill(MyDataset, "Parent")
        objDa1.Fill(MyDataset, "Child")

        titleRepeater.DataSource = MyDataset.Tables("Parent")
        titleRepeater.DataBind()
    End Sub


    Public Function GetDataSource(ByVal dataItem As Object) As DataView
        Dim i As Integer = 0
        Dim intNo As String = DataBinder.Eval(dataItem, "Item_No")
        Dim dv As DataView = MyDataset.Tables("Child").DefaultView
        dv.RowFilter = "Item_No='" & intNo & "'"
    End Function

'for some formatting...

Sub maingrid_showformatting(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        e.Item.Cells.RemoveAt(9)

        e.Item.Cells(1).Text = DataBinder.Eval(e.Item.DataItem, "calcLength", "{0:#,###}")
        e.Item.Cells(2).Text = DataBinder.Eval(e.Item.DataItem, "Width", "{0:#,###}")
end sub

works perfect...all the child datagrids are correctly populated with
inside the repeater...no problem there...

[red]can you please show me how to reference each dataview and where
to add/insert rows and do formatting...
[/red]

Thanks so much...

-DNG
 
one typo...i missed to put one line in the GetDataSource() function...

Code:
return dv

thanks

-DNG
 
you have made the task sound simple by putting it in two lines...
Code:
So you can simply get a reference to this from the ItemDataBound event by using the sender object.

but i am finding it hard to understand this and put it in code...

thanks

-DNG
 
I don't know what else I can do to explain it without writing the whole thing which it seems I'm starting to do...

The sender object of the ItemDataBound event e.g.
Code:
Sub maingrid_showformatting(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
is the DataGrid, so you simply have to cast the sender to a DataGrid and use the DataSource property to retreive the DataView.


____________________________________________________________

Need help finding an answer?

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

 
thanks so much ca8msm and sorry for the trouble...but believe me i have been doing lot of trials by searching code snippets online...but none worked...may be i am not getting the whole idea of these dynamic controls...

1. i tried viewing source and using the dynamically generated id of the child grids...
2. i tried looping through the repeater items
For Index As Integer = 0 To titleRepeater.Items.Count - 1
Next

and many other trials which might sound silly to you( thats why i am not mentioning it here...)

thanks again for all your assistance...

-DNG
 
As I've said, to get a reference to the DataView simply convert the DataSource:
Code:
CType(CType(sender, DataGrid).DataSource, DataView)


____________________________________________________________

Need help finding an answer?

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

 
thanks again ca8msm,

inside the itemDataBound sub...i have put like this:

Code:
Sub maingrid_showformatting(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

 Dim dv As DataView
  dv = CType(CType(sender, DataGrid).DataSource, DataView)

[red]inserting/adding rows code
Dim i As Integer = 0

        Do While i <= dv.Table.Rows.Count - 2
            For Each dr As DataRowView In dv
....
....
...
[/red]
end sub

is that correct...tested it but its taking long time...

thanks

-DNG
 
Some of my previous comments were:
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

ca8msm said:
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

I suggest you re-read some of the previous posts as I've pointed out that this is not a good idea...


____________________________________________________________

Need help finding an answer?

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

 
thanks again,

answer to your first comment...no i didnot have it inside ItemDataBound

here is the code again which shows that i had insert/adding rows loop before the databind in the BindData() sub shown in red...

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][b]'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
[/b][/red]
       'End inserting rows

        mydg.DataSource = ds
        mydg.DataBind()
    End Sub

 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
    If e.Item.Cells(0).Text.Equals("11111") Then 
     'doing some formatting here for that row
end if
End Select
 End sub

answer to the second comment...i thought we can format the datagrid only in the ItemDataBound sub

please suggest me if i am doing something wrong...thanks

-DNG

 
There's two problems here really.

The first is where you are actually looping through the DataSource. Originally (i.e. the code you've just posted) looped through the DataTable when the grid was bound and added some rows. You have sinced chanegd this to loop through the Table of the DataView in the ItemDataBound event.

You shouldn't be looping through the table in the ItenDataBound event as (and I've said this at least 3 or 4 times now) you would loop through your table everytime an item was bound.

What you should be doing is what you originally did, and loop the the original datasource before binding to the DataGrid. However, this then introduces a new problem. As we use a DataView to filter the rows, you'll have to make sure that when you add a new row, that it has the same ITEM_NO so that it appears in the correct child datagrid.


The second problem you have is related to the first (as it again loops through the table in the ItemDataBound event). You said "i thought we can format the datagrid only in the ItemDataBound sub" which is correct but your implementation of it is not. Again, what you do in your first example in the ItemDataBound event is correct (and if you look at my post dated 7 Jan 06 7:42 in thread855-1173378 you'll see that this is what I was trying to explain)




____________________________________________________________

Need help finding an answer?

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

 
ca8msm,

this is the article i have referred to which showed how to add/insert rows(sub headings) and i just did the same...


ok now i understood that i should not loop through the table of the dataview inside ItemDataBound sub...but i am not sure where exactly i should do this referring to the table of the dataview we want...do we need to create a new sub for this??

Next, from your below comment
ca8msm said:
What you should be doing is what you originally did, and loop the the original datasource before binding to the DataGrid. However, this then introduces a new problem. As we use a DataView to filter the rows, you'll have to make sure that when you add a new row, that it has the same ITEM_NO so that it appears in the correct child datagrid.

is this what are you referrering to...

Code:
    Public Function GetDataSource(ByVal dataItem As Object) As DataView
        Dim i As Integer = 0
        Dim intNo As String = DataBinder.Eval(dataItem, "Item_No")
        Dim dv As DataView = MyDataset.Tables("Child").DefaultView
        dv.RowFilter = "Item_No='" & intNo & "'" 

[red]'put the looping code here
'but not sure how that i am doing for the same ITEM_NO[/red]
        Return dv
    End Function

Thanks so much for your patience and guidance...

-DNG
 
ca8msm said:
What you should be doing is what you originally did, and loop the the original datasource before binding to the DataGrid
What you originally did, was loop through the table in the BindGrid sub, not the GetDataSource sub, so no that isn't correct.


____________________________________________________________

Need help finding an answer?

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

 
so you mean to say that we do not need the GetDataSource sub and i should have something like this:
Code:
Private Sub BindGrid()
        Dim strConnection As String
        strConnection = "my conn string"
        ...
        ...
        ...
        objDa.Fill(MyDataset, "Parent")
        objDa1.Fill(MyDataset, "Child")

        titleRepeater.DataSource = MyDataset.Tables("Parent")
        titleRepeater.DataBind()

[red][b] Dim i As Integer = 0
        Do While i <= MyDataset.Tables("Child").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
maingrid.DataSource=MyDataSet
maingrid.DataBind
[/b][/red]

    End Sub

does this work each child grid??...thanks a bunch

-DNG
 
ca8msm said:
However, this then introduces a new problem. As we use a DataView to filter the rows, you'll have to make sure that when you add a new row, that it has the same ITEM_NO so that it appears in the correct child datagrid.

this is exactly the trouble i am facing with from the beginning and unable to translate it to the code...i would really appreciate your help on this one...

-DNG
 
so you mean to say that we do not need the GetDataSource sub
No, I haven't ever mentioned getting rid of this sub as this is how each child DataGrid gets the relevant records so removing it would be pointless.

Think about this situation logically and in what order you need to approach them. You need to:

1) Get all of the records
2) Add the relevant sub header rows as required
3) When each parent is bound, get all the relevant child records
4) Apply any conditional formatting for both the parent and child records as the items are bound

Note: #3 and #4 may overlap slightly as you need to apply the parent's conditional formatting (if any) at the same time that you get the child records and then apply the childs conditional formatting.

Without meaning to sound harsh (and don't take this the wrong way), it seems fairly pointless if I simply provide you the whole code for you to copy and paste, as I've already shown you how to do steps 1,3 and 4, and you have simply pasted this code in without understanding it as you keep asking the same questions over and over again.

If you take the time to understand both the code and the comments I've made, then you won't need to repeat these questions and you will learn more about the code that you are putting into your application.


____________________________________________________________

Need help finding an answer?

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

 
Thanks ca8msm,

no you didnt sound harsh. i agree that i am in learning stages in asp.net...and whatever i had learned so far is from you, jim and other online resources...i did not find any example to the present situation i am having...or else if there was any sample code, i wud have made it work for my case...i have all thoughts in mind but just not coding it in the right place...

i understood what we need to do but just was having difficult to put in the code...i also tried to rewrite the whole thing using repeater's ItemDataBound and event handlers but wasnt successful either...so i sticked with the code you provided...

i will give another try but if you get some time...dont write the whole code but just some sample code would be helpful...

thanks

-DNG
 
ca8msm,

help me understand here...and correct me if i am doing something wrong...

1) Get all of the records
this gets us all the records:
Code:
objDa1.Fill(MyDataset, "Child")

2) Add the relevant sub header rows as required
Code:
'here we have our view now
dim mydv as dataview=objDa1.Fill(MyDataset, "Child").defaultview
'doing the looping code here...
Do while mydv<=.....
....

3) When each parent is bound, get all the relevant child records
this does the step 3
Code:
Dim dv As DataView = MyDataset.Tables("Child").DefaultView
dv.RowFilter = "Item_No='" & intNo & "'"

but the thing which i dont understand here is how can i refer mydv from step 2 and filter it for particular item in step 3??

4) Apply any conditional formatting for both the parent and child records as the items are bound

do i need here both maingrid's ItemDataBound and titleRepeater's ItemDataBound??

thanks so much

-DNG
 
ca8msm,

i tried until now without any luck...i would be thankful if you consider helping me out on this one...

thanks

-DNG
 
ca8msm,

thanks you for your help...i rewrote the whole page in asp in an hour...

i am still learning .net

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top