XML and Datagrid

a8le

Well-known member
Joined
Oct 27, 2005
Messages
75
Programming Experience
5-10
Hi all,

I am working on linking a XML file to a datagrid. I have enabled the datagrid to display the contents of the xml file using the datagrid.

But I want to be able to do more that to just display the content, I also want to be able to add new rows, edit existing rows, and delete existing rows. I am doing all this through the datagrid's edit,update,and delete feature.

Currently, I am working on the "insert_new_row" function and am able to insert a new row, but can not get a value into the new row. Instead, I get something like this...

VB.NET:
  <word xsi:nil="true" />

Here is the entry added to my original xml document...

VB.NET:
<?xml version="1.0" standalone="yes"?>
<root xmlns:xsi="[URL="http://www.w3.org/2001/XMLSchema-instance"]http://www.w3.org/2001/XMLSchema-instance[/URL]">
  <words>
    <word>value1</word>
    <word>value2</word>
    <word>value3</word>
    <word>etc</word>
  </words>
  <word xsi:nil="true" />
</root>

I have tried many thing but cannot get the the row to be
VB.NET:
<word>newword</word>
Here is my code...

VB.NET:
        Sub LoadXML()
            Dim objdata As New DataSet
            Try
                objdata.ReadXml(MapPath("Words.xml"))
                DataGrid1.DataSource = objdata
                DataGrid1.DataMember = "word"
                DataGrid1.DataBind()
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

VB.NET:
        Sub insert_new_row(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
            Try
                If E.CommandName = "doAdd" Then
                    Dim objdata As New DataSet
                    Dim tadd1 As TextBox
                    Dim dr As DataRow
 
                    objdata.ReadXml(MapPath("Words.xml")) 'fill in dataset
                    tadd1 = E.Item.FindControl("word_add_txt")  
 
                    objdata.Tables("word").DefaultView.RowFilter = ""
                    dr = objdata.Tables("word").NewRow()  
 
                    'dr(0) = tadd1.Text [B]--- I am trying to assign the value "newword" here but i get an error, so I commented it out and now get my current output. [/B]
                    objdata.Tables("word").Rows.Add(dr)
 
                    objdata.WriteXml(MapPath("Words.xml"))
                    LoadXML()
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

Any suggestion will help. Thank you in advance.

-a8le
 
The tadd1 is a textbox within the DataGrid Control, you cannot access it directly. Try something like this:

VB.NET:
dr(0) = CType(E.Item.ItemIndex.Cells(0).FindControl("tadd"), TextBox).Text

That code says to look in the datagrid for the Item (row) passed in as the DataGridCommandEventArgs and find the control named tadd in Cell 0. It takes a while to adjust to the fact that the TextBoxes in a DataGrid are not directly accessible.
 
No Go

Hi Brandon,

Thank you for the reply. I tried your code but I got an error saying that "Cells(0)" is not a part of "ItemIndex".

After playing around with the code, I finally had to take out "ItemIndex", because it seems "Cells" is a part of Items. The end result is that your code now looks like this (to not get any errors), but it still doesn't work....

VB.NET:
dr(0) = CType(E.Item.Cells(0).FindControl("word_add_txt"), TextBox).Text

Also within the FindControl(), I change "tadd" to "word_add_txt". Is that right, or should I have left it only?

The error I am still getting is....

VB.NET:
Object reference not set to an instance of an object. ---> 
System.NullReferenceException: Object reference not set to an instance of an object.

Any other suggestions?:D

-a8le
 
Within the FindControl Method you need to use the actual name of the TextBox control located within the EditItemTemplate of the DataGrid
 
Textbox

Schenz said:
Within the FindControl Method you need to use the actual name of the TextBox control located within the EditItemTemplate of the DataGrid

"word_add_txt" is the actual name of the textbox from my DataGrid...

Question: Don't these two lines from my original code

VB.NET:
                    tadd1 = E.Item.FindControl("word_add_txt")  
                    dr(0) = tadd1.Text

do the the exact same thing as your one line....

VB.NET:
dr(0) = CType(E.Item.Cells(0).FindControl("word_add_txt"), TextBox).Text

minus the "CType(object,type).type" ?

It seems even if the conversion statement is there, I am passing in nothing... giving me my error.

This is so fustrating. AAAAAAHHHHHHHH!!!!!!! Oh, thank you for helping. I really appreciated it. And, any other suggestions? :D

-a8le
 
I understand your frustration, and I think you right about the two lines being the same as the one line now.....

Maybe the problem is not in that side of the equation...

are you positive that the table name is "word"? Maybe try the index number instead to see what happens?

dr = objdata.Tables("word").NewRow()
becomes
dr = objdata.Tables(0).NewRow()
 
Hi Brandon,

That didn't work either. Can I ask you to do me a favor, kind sir? ... My project is the combination of these two tutorials found on the web...

http://aspnet.4guysfromrolla.com/articles/112603-1.aspx

and

http://www.dotnetjohn.com/articles.aspx?articleid=46

I got the replace bad words to work, I am trying now to edit (as in add, edit, update, and delete that list using one interface).

Can you look at the two article for me? I think it'll give you a better understanding of what it is that I am trying to do.

As you can see the structure of the XML documents are not the same. Do you think I should change the source for the replace bad words instead?

Thanks, a8le
 
Ok, I have an idea of what you are trying to accomplish. Is it possible for you to PM me the code you are working with? In that case I can try and see what is not quite working correctly for you.
 
change

hi brandon,

before sending you the code... can you please help me with this...

http://www.vbdotnetforums.com/showthread.php?t=6135

the reason I am asking that you look at this thread first is, if I can include the ( aid ) "an ID number" to the word list and am still able to replace words, my code will be changing. the reason is that there will be less code for me to change, if the "replace words function is changed."

-a8le
 
Back
Top