Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Xml Editor: Work with DataTable, DataView and DataGrid

0.00/5 (No votes)
2 Oct 2006 1  
Lets Work with XML, DataTable, DataView and DataGrid

Sample Image - Xml_Editor.jpg

Introduction

Currently, I work on a web design project. I created a MenuCreator which is based on XML files. Then, the client the XML files are hard to maintain, since he knows not much about computer. Therefore, I created a simple XML editor with datagrid

Work With....

XML, DataTable, DataView, DataGrid

Step:

1. Load DataTable from XML

2. Bind DataTable to DataGrid

3. Work with DataGrid

  • Insert a new row into n-position
  • Edit a data row in a datagrid
  • Delete a data row in a datagrid
  • Move a data row up
  • Move a data row down
  • The data row GOTO n-position

4. Write back to XML

CODE...

There are thousand samples with edit and delete a dataRow in datagrid samples on web

So...i just put Insert a new row into n-position and Move up,down and GOTO ON...

INSERT INTO

 private void addNewRow()
  {   
   if(this.isGoodIndex( TB_newDrPosition.Text))
   {
    int newRp = int.Parse(TB_newDrPosition.Text);
    DataTable dt = (DataTable) this.Session[this._SessionName];
    DataRow dr = dt.NewRow();
    dr["Id"] = newRp;
    dt.Rows.InsertAt(dr,newRp);
    this.reorderId(dt);
    this.Session["xmlTable"] =dt;
    DataView dv = dt.DefaultView;
    dv.Sort = "Id";
    this.newRowFlag.Value = "1";
    this.DataGrid1.DataSource = dv;
    this.DataGrid1.EditItemIndex = newRp;//dt.Rows.Count-1;

    this.DataGrid1.DataBind();
   }
   else
   {
    this.LWarning.Text = "<a class='warning' >" + 
                                             "Bad Index Input </a>";
   }
  }

Item MOVE MOVE MOVE AND GOTO

In this programe, moving row up and down is just switch rows around.

ItemCommand in DataGrid

private void DataGrid1_ItemCommand(object source, 
        System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   int selectedIndex = e.Item.ItemIndex;
   DataTable dt = this.takeOutDt();//(DataTable) this.Session[_SessionName];

   switch(e.CommandName.Trim())
   {
    case "SelectUp":
     if(selectedIndex == 0)
     {
      this.LWarning.Text = "<a class='warning' >" + 
         "Top row can not be moved up, there is no heaven</a>";
     }
     else
     {
      this.SwitchRows(dt,selectedIndex, selectedIndex-1);
     }
     break;
    case "SelectDown":
     if( selectedIndex == dt.Rows.Count-1)
     {
      this.LWarning.Text = "<a class='warning' >" + 
             "Botton Row can not be moved down, there is no hell</a>";
     }
     else
     {
      this.SwitchRows(dt,selectedIndex, selectedIndex+1);
     }
     break;
    case "SelectGoto":
     string _index = ((TextBox)e.Item.FindControl("gotowhere")).Text;
     if(this.isGoodIndex(_index))
     {
      int index = int.Parse(_index);
      this.dtGoTo(dt, selectedIndex, index);
     }
     break;
   }
   this.throwBackDt(dt);
   dTtoXml(dt, this._filePath,this._xmlStyle, this._XMLdtTag,this._XMLdrTag);
   DataView dv = dt.DefaultView;
   dv.Sort = "Id";
   this.DataGrid1.DataSource = dv;
   this.DataGrid1.DataBind();
  }

Switch rows

 private void SwitchRows(DataTable _dt, int i, int j)
  {
   object[] _iRow = _dt.Rows[i].ItemArray;
   object[] _jRow = _dt.Rows[j].ItemArray;
   _dt.Rows[i].ItemArray = _jRow;
   _dt.Rows[j].ItemArray = _iRow;
  }

GOTO

  private void dtGoTo(DataTable dt , int curI, int newI)
  {
   DataRow holder = copyRowAt(curI, dt);
   dt.Rows.RemoveAt(curI);
   this.reorderId(dt);
   dt.Rows.InsertAt(holder, newI);
   this.reorderId(dt);
   this.throwBackDt(dt);
   dTtoXml(dt, this._filePath,this._xmlStyle, this._XMLdtTag,this._XMLdrTag);
   this.DataGrid1.DataSource = dt.DefaultView;
   this.DataGrid1.DataBind();
  }

Thanks

Thanks for million of articles from web.

It is not really good code.....yet...

Thank you for Viewing...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here