locked
DataSet in GridView shows onyl part of the Xml-string RRS feed

  • Question

  • User485318798 posted

    I have a GridView that get's it's content from a DataSet.

    The problem is that it shows only one part of the xml-string:

    <SitePermissions>
              <Code>site1</Code>
              <DocTypes>
                <DocTypePermissions>
                  <Name>doctype1</Name>
                  <Evaluate>false</Evaluate>
                </DocTypePermissions>
                <DocTypePermissions>
                  <Name>doctype2</Name>
                  <Evaluate>true</Evaluate>
                </DocTypePermissions>
              </DocTypes>
              <DocumentView>true</DocumentView>
              <TestView>true</TestView>
            </SitePermissions>

    In this example the GridView doesn't show Name and Evaluate, but only DocumentView and TestView.

    Is this because DataSet picks only DocumentView and TestView?

    This is what i use:

    MyGridView.DataSource = dataSet.Tables[3];
    			MyGridView.DataBind();

    What should i as a parameter for Tables[?]?

    In aspx this is how i call the TestView:

    <asp:TemplateField HeaderText="Listenansicht">
                        <ItemTemplate>
                            <asp:CheckBox ID="chkStatusList" runat="server" Checked='<%#Eval("TestView").ToString().Equals("true") %>'
                                AutoPostBack="false"/>
                        </ItemTemplate>                   
                    </asp:TemplateField> 

    How can i do the same for /DocType/DocTypePermissions/Name ???

    Thx

    Monday, September 23, 2013 3:16 AM

Answers

  • User697462465 posted

    Hi Tomiu,

    I suggest you wo use XMLDataSource control and XPath to solve it.

    There has a sample code, please try to refer to it:
    In the aspx file:

    <asp:XmlDataSource ID="sourceDVD" runat="server"></asp:XmlDataSource>
    
                    <asp:GridView ID="GridView1"
                        runat="server"
                        AutoGenerateColumns="False"
                        BorderStyle="Groove"
                        BorderWidth="2px"
                        CellPadding="4"
                        ForeColor="#333333"
                        GridLines="None"
                        Width="279px">
    
                        <Columns>
    
                            <asp:TemplateField HeaderText="DVD">
    
                                <ItemTemplate>
    
    
    
                                    <b><%#XPath("./Title")%></b><br />
    
                                    <%#XPath("./Director")%><br />
    
                                    <br />
                                    <i>Starring...</i><br />
    
                                    <blockquote>
    
                                        <asp:GridView ID="GridView2" ShowHeader="False" GridLines="None" AutoGenerateColumns="False" DataSource='<%# XPathSelect("./Starring/Star") %>' runat="server">
    
                                            <Columns>
    
                                                <asp:TemplateField>
    
                                                    <ItemTemplate>
    
                                                        <%#XPath(".")%><br />
    
                                                    </ItemTemplate>
    
                                                </asp:TemplateField>
    
                                            </Columns>
    
                                        </asp:GridView>
    
                                    </blockquote>
    
                                </ItemTemplate>
    
                            </asp:TemplateField>
    
    
    
                        </Columns>
    
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    
                        <RowStyle BackColor="#EFF3FB" />
    
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    
                        <EditRowStyle BackColor="#2461BF" />
    
                        <AlternatingRowStyle BackColor="White" />
    
                    </asp:GridView>

    In the aspx.cs file:

    sourceDVD.Data = @"<DvdList><DVD ID='1' Category='Category 1'> <Title>title 1</Title><Director>directory 2</Director><Price>1</Price><Starring><Star>star 1</Star><Star>star 2</Star></Starring></DVD><DVD ID='2' Category='Category 2'><Title>title 2</Title><Director>directory 2</Director><Price>2</Price><Starring><Star>star 3</Star><Star>star 4</Star></Starring></DVD></DvdList>";
                GridView1.DataSource = sourceDVD;
                GridView1.DataBind();

    You can get the xml string from database, and set it to XMLDataSource control, then bind it to GridView.

    Hope it can help you.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 10:31 PM

All replies

  • User-1623675128 posted

    why dont you check the Tables[3] content in quickwatch?> so that we can see if the tables[3] carrying complete XML or not; if not carrying its someother issue..

    i doubt if xml is coming into a single row as a text from the DB. 

    Tuesday, September 24, 2013 1:01 AM
  • User485318798 posted

    I created a new XML:

    <Permissions>
    <Studies>
        <StudyPermissions>
          <Code>Test1</Code>
          <Sites>
            <SitePermissions>
              <Code>site1</Code>
              <DocTypes>
                <DocTypePermissions>
                  <Name>doctype1</Name>
                </DocTypePermissions>
                <DocTypePermissions>
                  <Name>doctype2</Name>
                </DocTypePermissions>
              </DocTypes>
              <DocumentView>true</DocumentView>
              <TestView>true</TestView>
            </SitePermissions>
    	  </Sites>
    	</StudyPermissions>
    </Studies>
    </Permissions>

    Tables[0], Tables[3] with quickwatch show only 4 Tables: Studies,StudiePermissions,Site,SitePermissions. With Eval  i can show the content for the nodes: Code, DocumentView and TestView but not for "Name".

    This is how i create the Dataset:

    StringReader sr = new StringReader(Data);
    XmlTextReader xtr = new XmlTextReader(sr);
    DataSet dataset = new DataSet();
    dataset.ReadXml(xtr);


    Tuesday, September 24, 2013 3:33 AM
  • User-1623675128 posted

    You will get 6 tables with this data as DocType & DocTypePermissions are also elements.

    if not creating then there is problem with "data" paramter in your code; I would suggest directly pass the file to the XMLTextReader and avoid StringReader.

    XmlTextReader xtr = new XmlTextReader("file.xml");

    DataSet dataset = new DataSet();

    dataset.ReadXml(xtr);

     

    hope this will help!

    Tuesday, September 24, 2013 6:53 AM
  • User485318798 posted

    Well the problem is that i dont have xml files only xml strings :(. Is there any other approach that exludes using the StringReader? I create the xml string inside the code.

    Thank you

    Tuesday, September 24, 2013 7:23 AM
  • User-1623675128 posted

    I have tried your same code its working great...

    string xml = "<Permissions><Studies> <StudyPermissions><Code>Test1</Code><Sites> <SitePermissions><Code>site1</Code> <DocTypes><DocTypePermissions><Name>doctype1</Name></DocTypePermissions><DocTypePermissions><Name>doctype2</Name></DocTypePermissions></DocTypes><DocumentView>true</DocumentView><TestView>true</TestView></SitePermissions></Sites></StudyPermissions></Studies></Permissions>"

    ;StringReader sr = new StringReader(xml);

    XmlTextReader xtr = new XmlTextReader(sr);

    DataSet dataset = new DataSet();

    dataset.ReadXml(xtr);

    the data set has 6 tables whihc includes NAME tag. make sure you are getting teh same XML that is posted here from your previous lines of code..

    Tuesday, September 24, 2013 8:23 AM
  • User485318798 posted

    I tested the string as you have wrotte and now get 6 Tables, but how to enter the value for Name? With:

    GridView1.DataSource = dataset.Tables[3];
    GridView1.DataBind();

    I get same results as before with Eval. This works:

     <asp:TemplateField HeaderText="TestView">
                        <ItemTemplate>
                            <asp:CheckBox ID="testBox" runat="server" Checked='<%#Eval("TestView").ToString().Equals("true") %>'
                                AutoPostBack="true" />
                        </ItemTemplate>                                      
                    </asp:TemplateField>  

    But not this:

     <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:CheckBox ID="nameList" runat="server" Checked='<%#Eval("Name").ToString().Equals("true") %>'
                                AutoPostBack="true" />
                        </ItemTemplate>                                      
                    </asp:TemplateField> 

     <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:CheckBox ID="nameList" runat="server" Checked='<%#Eval("DocTypePermissions/Name").ToString().Equals("true") %>'
                                AutoPostBack="true" />
                        </ItemTemplate>                                      
                    </asp:TemplateField>  

    I tried many other possibilities like: DocType/DocTypePermissions/Name but always comes error message: DataBinding: System.Data.DataRowView contains no Property for Name.


    If i use :

    GridView1.DataSource = dataset;

    then i can't access any Node.

    Thank you

    Wednesday, September 25, 2013 4:17 AM
  • User-1623675128 posted

    See you have 6 tables of dataset then you cant get them into the data grid view directly with Eval (at least I dont know).

    possblle solution that I could think off is, you need to create one main table with all possible coloumns and merge all tables into main table then use the main table as source to your gridview. (this could have been done easily in SQL if the data is coming from SQL).

     

    sorry not much helpfull!

    Wednesday, September 25, 2013 7:04 AM
  • User485318798 posted

    Data is coming from a Database but as an xml-string.

    Wednesday, September 25, 2013 7:31 AM
  • User-1623675128 posted

    then you should write join to get the data from diff tables into single select statment so that XML will have only element with multiple rows which can be binded to DataSet when loaded into data set.

     

    is the data coming from difftables or this XML is part of a column in table?.. need more details to get this nailed down. (how the XML is formed and sent to C#)

    if the XML is coming from a table column then we cant do much about it then left only option merge all these 6 tables in C# manually..

    Wednesday, September 25, 2013 8:04 AM
  • User485318798 posted

    It's a column in DB that contains the xml-string. I save the xml as string in that column

    Wednesday, September 25, 2013 8:09 AM
  • User697462465 posted

    Hi Tomiu,

    I suggest you wo use XMLDataSource control and XPath to solve it.

    There has a sample code, please try to refer to it:
    In the aspx file:

    <asp:XmlDataSource ID="sourceDVD" runat="server"></asp:XmlDataSource>
    
                    <asp:GridView ID="GridView1"
                        runat="server"
                        AutoGenerateColumns="False"
                        BorderStyle="Groove"
                        BorderWidth="2px"
                        CellPadding="4"
                        ForeColor="#333333"
                        GridLines="None"
                        Width="279px">
    
                        <Columns>
    
                            <asp:TemplateField HeaderText="DVD">
    
                                <ItemTemplate>
    
    
    
                                    <b><%#XPath("./Title")%></b><br />
    
                                    <%#XPath("./Director")%><br />
    
                                    <br />
                                    <i>Starring...</i><br />
    
                                    <blockquote>
    
                                        <asp:GridView ID="GridView2" ShowHeader="False" GridLines="None" AutoGenerateColumns="False" DataSource='<%# XPathSelect("./Starring/Star") %>' runat="server">
    
                                            <Columns>
    
                                                <asp:TemplateField>
    
                                                    <ItemTemplate>
    
                                                        <%#XPath(".")%><br />
    
                                                    </ItemTemplate>
    
                                                </asp:TemplateField>
    
                                            </Columns>
    
                                        </asp:GridView>
    
                                    </blockquote>
    
                                </ItemTemplate>
    
                            </asp:TemplateField>
    
    
    
                        </Columns>
    
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    
                        <RowStyle BackColor="#EFF3FB" />
    
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    
                        <EditRowStyle BackColor="#2461BF" />
    
                        <AlternatingRowStyle BackColor="White" />
    
                    </asp:GridView>

    In the aspx.cs file:

    sourceDVD.Data = @"<DvdList><DVD ID='1' Category='Category 1'> <Title>title 1</Title><Director>directory 2</Director><Price>1</Price><Starring><Star>star 1</Star><Star>star 2</Star></Starring></DVD><DVD ID='2' Category='Category 2'><Title>title 2</Title><Director>directory 2</Director><Price>2</Price><Starring><Star>star 3</Star><Star>star 4</Star></Starring></DVD></DvdList>";
                GridView1.DataSource = sourceDVD;
                GridView1.DataBind();

    You can get the xml string from database, and set it to XMLDataSource control, then bind it to GridView.

    Hope it can help you.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 10:31 PM