I have a very basic question.

I am populating a datagrid from an XML file, ie. binding datagrid to XML

The datagrid has 4 template columns - ID (label), Episode Name (label), Part A (checkbox), Part B (checkbox).

What I want to do is remove the nodes "<name>" from the XML and statically populate the "Episode Name" column of the datagrid using hard-coded values.

Can I do it at the same time when the XML is getting bound to the datagrid?

The Episodes XML is as follows -

<?xml version="1.0" standalone="yes"?>
<CMS>
<Episode>
<name>Communicating Effectively</name>
<partOne>0</partOne>
<partTwo>0</partTwo>
</Episode>
<Episode>
<name>Managing Conflict</name>
<partOne>0</partOne>
<partTwo>0</partTwo>
</Episode>
</CMS>

Thanks in advance,
Aditi

ok i created a working example for you, files are default.aspx, default.aspx.cs, XMLFile.xml.

default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

default.aspx.cs :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("XMLFile.xml"));
        DataTable dt = ds.Tables[0];
        //start hardcoding here
        dt.Rows[0]["name"] = "serkan";
        dt.Rows[1]["name"] = "sendur";

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

XMLFile.xml :

<?xml version="1.0" standalone="yes"?>
<CMS>
  <Episode>
    <name>Communicating Effectively</name>
    <partOne>0</partOne>
    <partTwo>0</partTwo>
  </Episode>
  <Episode>
    <name>Managing Conflict</name>
    <partOne>0</partOne>
    <partTwo>0</partTwo>
  </Episode>
</CMS>

mark as solved if this solves your problem.

Hey thanks a lot.. Will try and let you know!!

-Aditi

Hey !
I tried this -

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   
        Dim ds As New DataSet
        Dim dt As New DataTable
        dt = ds.Tables("Episodes")
        'start hardocing here
        dt.Rows(0)("name") = "Managing conflict"
        dt.Rows(1)("name") = "Faculty expertise"

        dgWebisodes.DataSource = dt
        dgWebisodes.DataBind()

 End Sub

But I am getting this error - Object reference not set to an instance of an object. at (dt.Rows(0)("name") = "Managing conflict"
)

Also, I want to remove the <name> tag completely from the XMl so the XML would look like this -

<?xml version="1.0" standalone="yes"?>
<CMS>
  <Episode>
    <partOne>1</partOne>
    <partTwo>1</partTwo>
  </Episode>
  <Episode>
    <partOne>1</partOne>
    <partTwo>1</partTwo>
  </Episode>
 </CMS>

Also, only the <partOne> and <partTwo> tags will be read from the XML.

So the datagrid would look something like this -

Episode Name | Part A | Part B
==========================================
Managing conflict | checkbox | checkbox
Faculty expertise | checkbox | checkbox

where is this portion ? ds.ReadXml(Server.MapPath("XMLFile.xml"));

Sorry, it was missing, but still it is giving me the same error.

AND I do not want the <name> node to be in the XML.
I am populating the other information as follows -

My whole code goes like this -


Default.aspx
=========

<asp:datagrid id="dgWebisodes" runat="server" Font AutoGenerateColumns="False">
            <Columns>
       <asp:TemplateColumn HeaderText="Webisode Name">
            <ItemTemplate>
             <asp:Label runat="server" text ='<%# DataBinder.Eval(Container, "DataItem.name") %>' Visible="true" ID="Episodename"> </asp:Label>
              </ItemTemplate>
             </asp:TemplateColumn>
    
  <asp:TemplateColumn HeaderText="Part A" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:CheckBox ID="partOne_Edit" Runat="server" Enabled = "true" Checked ='<%# DataBinder.Eval(Container, "DataItem.partOne") %>'/>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>

             </asp:TemplateColumn>

            <asp:TemplateColumn HeaderText="Part B" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:CheckBox ID="partTwo_Edit" Runat="server" Enabled="true" Checked ='<%# DataBinder.Eval(Container, "DataItem.partTwo") %>' />
                        </ItemTemplate>
                       <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateColumn>
               </Columns>
               
</asp:datagrid>

Default.aspx.vb
===========

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Xml.XPath
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Security
Imports Microsoft.VisualBasic
Partial Class _Default
  Inherits System.Web.UI.Page
    Public lastVarValue As Integer
    Dim objdata As New DataSet
    Dim dt As New DataTable("Episode")
    Protected Sub 
Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Dim ds As New DataSet
        ds.ReadXml(Server.MapPath("~/cmsresources/episodes.xml"))
        Dim dt As New DataTable
        dt = ds.Tables("Episodes")

        'start hardocing here
        dt.Rows(0)("name") = "Managing conflict"
        dt.Rows(1)("name") = "Faculty expertise"

        dgWebisodes.DataSource = dt
        dgWebisodes.DataBind()

        If Not IsPostBack Then
          LoadXML()
        End If
    End Sub
    Sub LoadXML()
         '' Tell the datareader which file to read from
        objdata = New DataSet
        objdata.ReadXml(Server.MapPath ("~/cmsresources/episodes.xml"))
        
        dt.Columns.Add(New DataColumn("name"))

        If Not Page.IsPostBack Then
            BindEpisodes()        End If
    End Sub

    Sub BindEpisodes()
        Dim dv As New DataView(objdata.Tables("Episodes"))

        dgWebisodes.DataSource = objdata
        dgWebisodes.DataBind()    End Sub


    Function ShowVal(ByVal a As Integer)
        lastVarValue = a
        Return a   End Function

    Sub saveepisodes()
        objdata.WriteXml(Server.MapPath("~/cmsresources/episodes.xml"))
    End Sub

   Protected Sub save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles save.Click
        'Loop through the items in the datagrid
        Dim dgitem As DataGridItem
        objdata = New DataSet
        objdata.ReadXml(Server.MapPath("~/cmsresources/episodes.xml"))
       For Each dgitem In dgWebisodes.Items
            Dim intRow As Integer = dgitem.ItemIndex
            If intRow <> -1 Then
                'Make sure this is an item and not the header or footer
                If dgitem.ItemType = ListItemType.Item Or dgitem.ItemType = ListItemType.AlternatingItem Then

                    '' Get the current row for update or delete operations later.
                    Dim ENAME As Label = CType(dgitem.Cells(0).FindControl("Episodename"), Label)

                    Dim ptOne As CheckBox = CType(dgitem.Cells(1).FindControl("partOne_Edit"), CheckBox)
                    Dim ptTwo As CheckBox = CType(dgitem.Cells(2).FindControl("partTwo_Edit"), CheckBox)             
 If ptOne.Checked = True Then
                        objdata.Tables(0).Rows(intRow)("partOne") = 1
                    Else
                        objdata.Tables(0).Rows(intRow)("partOne") = 0
                    End If

                    If ptTwo.Checked = True Then
                        objdata.Tables(0).Rows(intRow)("partTwo") = 1
                    Else
                        objdata.Tables(0).Rows(intRow)("partTwo") = 0
                    End If                End If
            End If
         
        Next
        If objdata.HasChanges Then
            saveepisodes()

        End If
        BindEpisodes()
    End Sub

Episodes.xml
=========

<?xml version="1.0" standalone="yes"?>
<CMS>
  <Episode>
    <partOne>1</partOne>
    <partTwo>1</partTwo>
  </Episode>
  <Episode>
    <partOne>1</partOne>
    <partTwo>1</partTwo>
  </Episode>
</CMS>

oh man , i gave you the main idea, i am at work, i can not read all your code now, you must wait until i get home.

Thanks!
but I would be grateful to you if you go through my code and suggest any more changes.

-Aditi

ok, first using C# to VB converters, convert my example code and then run it. Then using similar approach try to build your own application, divide and manage, so you will have smaller questions about the errors in your code. i will try to read it when i get home.

ok, first using C# to VB converters, convert my example code and then run it. Then using similar approach try to build your own application, divide and manage, so you will have smaller questions about the errors in your code. i will try to read it when i get home.

Yes I tried your code and it's working good.
But the problem with it is that I tried using the same code but if I remove the <name> tags from the XML it does not work as it has to have a datasource column. And in my case I want to get rid of the <name> column.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.