2013年7月22日星期一

Read on XML Binding to Gridview

 
gvDevice.DataSource = ds.ReadXml (@ "D: \ projects \ Dongpang _device.xml"); gvDevice.DataBind ();


write can do?
------ Solution ---------------------------------------- ----
Oh, okay you try not on
------ Solution ----------------------- ---------------------
http://www.cnblogs.com/hulang/archive/2011/01/11/1932568.html

In fact, a lot of examples online

more Soso on the line
------ Solution ------------------------------- -------------
1, XML data source file named Table.xml, structured as follows:


0.40
0.40
0.40
0.20
0
0.40
0
0.05
L-0.2
0.18
0.37


2.54
0.70
0.80
0.60
1
0.30
1
0.05
L
0.50
0.80


1005
0.50
0.60
0.40
0.20
0.05
0.05
0.53
0.45


1608
0.70
0.90
0.80
0.30
0.05
0.05
0.85
0.60


5750
1.50
5.30
4.10
0.70
0.15
0.05
5.20
1.30



2, the specific code

string xmlPath = "Calculation / Tables.xml";

/ / resume normal state GridView
private void ResetParameter ()
{
gvParameter.EditIndex = -1;
LoadParameter (ddlShapeType.SelectedValue);
}

/ / load the XML and displayed in the GridView
private void LoadParameter (string shapeType)
{
DataSet dsRule = new DataSet ();
dsRule.ReadXml (xmlPath);

DataTable dtRule = dsRule.Tables [shapeType];

gvParameter.DataSource = dtRule;
gvParameter.DataBind ();
}

/ / add an event
protected void lnbAddNew_Click (object sender, EventArgs e)
{
if (ddlShapeType.SelectedValue.Length == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "Warning: Please select shape type!";
return;
}
else if (gvParameter.Rows.Count == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "Warning: [" + ddlShapeType.SelectedValue + "] table not exists!" ;
return;
}

DataSet ds = new DataSet ();
ds.ReadXml (xmlPath);
DataTable dt = ds.Tables [ddlShapeType.SelectedValue];

DataRow dr = dt.NewRow ();
dt.Rows.Add (dr);

ds.WriteXml (xmlPath); / / writes the modifications Table.xml

ResetParameter ();
}

/ / Edit Event
protected void gvParameter_RowEditing (object sender, GridViewEditEventArgs e)
{
gvParameter.EditIndex = e.NewEditIndex;
LoadParameter (ddlShapeType.SelectedValue);

GridViewRow row = gvParameter.Rows [e.NewEditIndex];

/ / front two are Delete, Edit, and therefore start from the third row
for (int i = 2; i {
TextBox tb = (TextBox) row.Cells [i]. Controls [0];
tb.Width = 50;
}
}

/ / update events
protected void gvParameter_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvParameter.Rows [e.RowIndex]; / / get the current row

int numCell = row.Cells.Count; / / of a few column cells (includes Edit and Delete 2 columns)
int currentRow = row.DataItemIndex; / / corresponding row index corresponds to the DataSet

DataSet ds = new DataSet ();
ds.ReadXml (xmlPath);
DataRow dr;

dr = ds.Tables [ddlShapeType.SelectedValue]. Rows [row.DataItemIndex]; / / find the corresponding line with the DataSet

string [] str = null; / / This array defines the table column name

switch (ddlShapeType.SelectedValue)
{
case "SOP":
{
str = new string [] {"PitchY", "T", "B", "WM", "WM1" ;, "WP", "WP1", "R", "LM", "WCM", "WCP"};
break;
}
case "DCHIP":
{
str = new string [] {"BodySize", "L", "W", "D", ; "T", "S", "R", "Wm", "Lm"};
break;
}
}

int j = 0;
/ / start from the first three, in front two are Edit and Delete
for (int i = 2; i {
string cText = ((TextBox) row.Cells [i]. Controls [0]). Text;

dr [str [j]] = cText;

j + +;
}

ds.WriteXml (xmlPath); / / writes the modifications Table.xml

ResetParameter ();
}

/ / undo changes
protected void gvParameter_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
{
ResetParameter ();
}

/ / remove events
protected void gvParameter_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = gvParameter.Rows [e.RowIndex];

int curr = row.RowIndex;
DataSet ds = new DataSet ();
ds.ReadXml (xmlPath);

DataRow dr = ds.Tables [ddlShapeType.SelectedValue]. Rows [curr];
dr.Delete ();
ds.WriteXml (xmlPath);

ResetParameter ();
}
------ Solution ------------------------------- -------------
xml operation class

can be read into the node and then bind the DataTable. .
------ For reference only -------------------------------------- -




how to get the number of nodes XMl

------ For reference only ---------------------------------- -----
 




How to Get
number of nodes
------ For reference only ------------------------ ---------------




trouble to give a sample code it
...

没有评论:

发表评论