Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: XML from Datagrid


Message #1 by "Paul Riley" <paul.riley@s...> on Mon, 30 Dec 2002 15:44:54 -0000
OK I am getting to my wits end now :)
 
All I want to do is to write XML from the data contained within controls
on a datagrid (I have a datagrid with checkboxes, combo boxes and input
boxes all of which I need to go into an xml file)
 
I am not overly bothered what format at the moment as everything else
can be built up round this one feature.
 
Is there any way to parse though the container on postback?
 
I need all changes to be written in one swoop so I need to iterate
through the rows in the datagrid. 
 
I know there must be a way to do all of this but no matter what I try I
seem to continuously end up hitting a brick wall - any help would be
greatly appreciated

Message #2 by "Lewis Bass" <lewis@t...> on Mon, 30 Dec 2002 10:56:34 -0800
I do this all of the time. I love the datagrid, but the paradym of changing
one row at a time does not work for our application. It is simply too slow
for
data entry.

I setup the datagrid from an SQL database with each column being a template
column!
and exampl of My HTML is below :

<asp:datagrid id="dgTable" runat="server" GridLines="Vertical"
CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None"
BorderColor="#999999" AutoGenerateColumns="False">
							<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#008A8C"></SelectedItemStyle>
							<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
							<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
							<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#000084"></HeaderStyle>
							<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
							<Columns>
								<asp:TemplateColumn>
									<ItemTemplate>
										<asp:TextBox id="tb0" runat="server" Text='<%# RenderTextBox("0",
Container) %>' Width="100%">
										</asp:TextBox>
									</ItemTemplate>
								</asp:TemplateColumn>

Notice that I am using the itemtemplate template!

then when a user clicks on a button somewhere on the form I go through the
datagid and save each row.
The trick to making this work is that all of the rows are deleted first, and
then the new rows are inserted.
I do not use an update on anything contained within the datagrid. This frees
me from having to worry about
new rows being added or deleted. I use code similiar to this below:


     ' delete the existing rows
        qry = "delete from " & Trim(Session("sysSingleTable_TableName"))
        If Trim(filterwhere) <> vbNullString Then
            qry = qry & " where " & filterwhere
        End If
        If tcu.Execute_Command(qry) = False Then
            lblERROR.Text = "ERROR Can Not Delete Old Entries"
            Exit Sub
        End If

        ' add the new rows
        col = Session("sysSingleTable_ColumnArray")
        For i = 0 To (dgTable.Items.Count - 1) ' walk through each row on
the table
            qry = "insert into " & Trim(Session("sysSingleTable_TableName"))
& "("
            qryname = ""
            qryvalue = ""
            okrun = False
            ' are we deleting the row?
            If CType(dgTable.Items(i).FindControl("cbDelete"),
CheckBox).Checked = False Then
                For j = 0 To Session("sysSingleTable_ColumnCount") ' walk
through each column on the row
                    If Trim(tcu.NZ(col(j, 0))) <> vbNullString Then
                        fieldname = Trim(tcu.NZ(col(j, 0)))
                        If UCase(Trim(tcu.NZ(col(j, 2)))) = "TEXT" Or
UCase(Trim(tcu.NZ(col(j, 2)))) = "DATE" Then
                            ctrlname = "tb" & Trim(j.ToString)
                            fieldvalue 
Trim(tcu.NZ(CType(dgTable.Items(i).FindControl(ctrlname), TextBox).Text))
                        ElseIf UCase(Trim(tcu.NZ(col(j, 2)))) = "DD" Then
                            ctrlname = "dd" & Trim(j.ToString)
                            fieldvalue 
Trim(tcu.NZ(CType(dgTable.Items(i).FindControl(ctrlname),
DropDownList).SelectedItem.Value))
                        ElseIf UCase(Trim(tcu.NZ(col(j, 2)))) = "CB" Then
                            ctrlname = "cb" & Trim(j.ToString)
                            If CType(dgTable.Items(i).FindControl(ctrlname),
CheckBox).Checked Then
                                fieldvalue = Trim(tcu.NZ(col(j, 3)))
                            Else
                                fieldvalue = Trim(tcu.NZ(col(j, 4)))
                            End If
                        End If

                        If Trim(qryname) = vbNullString Then
                            qryname = qryname & fieldname
                            qryvalue = qryvalue & "'" & fieldvalue & "'"
                        Else
                            qryname = qryname & ", " & fieldname
                            qryvalue = qryvalue & ",'" & fieldvalue & "'"
                        End If
                        If Trim(fieldvalue) <> vbNullString Then
                            okrun = True
                        End If
                    End If
                Next j
                ' finish putting together the qry string
                If okrun Then
                    If Trim(filtername) = vbNullString Then
                        qry = qry & qryname & ") values (" & qryvalue & ")"
                    Else
                        qry = qry & qryname & "," & filtername & ") values
(" & qryvalue & "," & filtervalue & ")"
                    End If

                    If tcu.Execute_Command(qry) = False Then
                        lblERROR.Text = "ERROR: Can Not Insert New Record"
                        Exit Sub
                    End If
                End If
            End If  ' is cbDelete checked?
        Next i


I have done this multiple times in my application. I hope that this helps



-----Original Message-----
From: Paul Riley [mailto:paul.riley@s...]
Sent: Monday, December 30, 2002 7:45 AM
To: ASP.NET
Subject: [aspx] XML from Datagrid


OK I am getting to my wits end now :)

All I want to do is to write XML from the data contained within controls
on a datagrid (I have a datagrid with checkboxes, combo boxes and input
boxes all of which I need to go into an xml file)

I am not overly bothered what format at the moment as everything else
can be built up round this one feature.

Is there any way to parse though the container on postback?

I need all changes to be written in one swoop so I need to iterate
through the rows in the datagrid.

I know there must be a way to do all of this but no matter what I try I
seem to continuously end up hitting a brick wall - any help would be
greatly appreciated




  Return to Index