 |
|
|
 |
 |
|
ทำความเข้ากันก่อนนะว่า xml = string
เรามีตาราง customer ซึ่งมี 2 field คือ
1. ID (pk, int)
2. Information (xml)
เรา query field Information มาเก็บไว้ในตัวแปร string ก่อน จากนั้นก็พยายามแปลง string xml นั้นให้ DataTable ให้ได้ โดยใช้
Code (C#)
//สมมติ xml string ที่ query มาแล้ว เราเก็บไว้ในตัวแปรที่ชื่อ xmlString นะ
System.IO.TextReader Tr = new System.IO.StringReader(xmlString);
DataSet Ds = new DataSet();
Ds.ReadXml(Tr);
DataTable Dt = Ds.Tables[0];
เราก็จะได้ Dt ที่มีข้อมูลของ xml นั้น จากนั้นเราจะเอาไปทำอะไรก็ง่าย
ไม่ว่าจะแสดงใน GridView
Code (C#)
GridView1.DataSource = Dt;
GridView1.DataBind();
หรือ Insert, Update, Delete ก็ทำได้ (โดยทำกับ DataTable Dt)
ก็กระทำชำเรา Dt จนหนำใจก็แปลงจาก DataTable กับเป็นเป็น string อีกครั้งเพื่อเก็บลงฐานข้อมูล
Code (C#)
System.IO.TextWriter Wr = new System.IO.StringWriter();
Dt.WriteXml(Wr);
string xmlString = Wr.ToString();
|
 |
 |
 |
 |
Date :
2009-12-31 00:51:23 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคุณ tungman มากเลยนะครับ สำหรับคำแนะนำดีๆ
ภายหลังจาพยายามศึกษาเพิ่มเติม
รายงานความก้าวหน้าครับ ตอนนี้ผมทำให้แสดงที่ GridView ได้แล้วและ Edit ได้แล้วอ่าครับ
ด้วยโค้ดดังนี้อ่าคับ
Code (C#)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable Dt = Session[key] as DataTable;
informationDataContext z = new informationDataContext();
DataSet Ds = new DataSet();
if (Dt == null)
{
Dt = new DataTable();
Ds = new DataSet();
var query = from a in z.jubzs where select a.xml;
var xmlstring = from q in query select q.ToString();
foreach (string h in xmlstring)
{
System.IO.TextReader Tr = new System.IO.StringReader(h);
Ds.ReadXml(Tr);
}
Dt.Columns.Add("Name", typeof(string));
Dt.Columns.Add("City", typeof(string));
Dt.Columns.Add("Age", typeof(string));
Dt = Ds.Tables[0];
Session[key] = Dt;
}
return Dt;
}
protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
TextBox cityty = GridView1.Rows[e.RowIndex].FindControl("txtCity") as TextBox;
TextBox ageage = GridView1.Rows[e.RowIndex].FindControl("txtAge") as TextBox;
string newcity = cityty.Text;
string newAge = ageage.Text;
DataTable Dt = GetDataSource();
Dt.Rows[e.RowIndex][1] = newcity;
Dt.Rows[e.RowIndex][2] = newAge;
GridView1.EditIndex = -1;
BindGrid();
}
}
คือมันสามารถแสดงได้อ่ะครับ แต่ผมอยากให้มันแสดงค่า id ที่เป็น int ไปพร้อมๆกันใน GridView เลยอ่ะครับ
ผมทำตรงใส่ค่า id ไปพร้อมๆกับที่แปลง xml format เป็น string เข้าไปข้างใน dataset ไม่เป็นนะครับ
แล้วที่บอกว่าให้ทำ datatable เป็น string แล้วค่อยเก็บอ่าครับ คือไม่ทราบว่าทำให้เป็นในรูปแบบ xml ก่อนเก็บยังไงอ่าครับ
รบกวนด้วยนะครับ
|
 |
 |
 |
 |
Date :
2009-12-31 14:45:51 |
By :
nutto54 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งงครับ ช่วยอธิบายที ผมไม่เข้าใจว่าจะเอา id ไปวางไว้ตรงไหนของ GridView 
ที่เข้าใจตอนนี้คือ id แทนตาราง xml หนึ่งตาราง เช่น id=1 มีข้อมูลหนึ่งตาราง id=2 มีข้อมูลอีกหนึ่งตาราง
ซึ่งถ้าเป็นไปตามที่เข้าใจ เอา id ไปแปะไว้ที่ label เป็นชื่อของตารางนั้นดีกว่า
|
 |
 |
 |
 |
Date :
2009-12-31 17:48:55 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Table จริงๆผมมีสอง Field นั่นคือ Id และ Information
ึคือจากโค้ดด้านบนอ่าครับ คือเอาข้อมูลจาก Field ที่สองนั่นคือ Information ที่เป็น xml type
แล้วไปใส่ใน GridView ได้แล้ว แสดงออกมา 3 column ตามข้อมูลใน xml
แต่ที่บอกว่าเอา id ไปด้วยอ่าครับ คืออยากให้ GridView มี 4 Column
Column แรกคือเป็น id ที่เป็นข้อมูลใน Field แรกอย่างที่บอก
Column อื่นอีก 3 column ให้เป็นข้อมูลตาม xml
ผมไม่แน่ใจว่าต้องจัดการที่ dataset หรือ datatable นะคับ
คือผมทำให้มันไปรวมกันก่อนไปผูกกับ GridView ไม่เปนอ่าครับ
งงป่าวอ่าครับ ผมกลัวอธิบายไม่ชัดเจน
รบกวนด้วยนะครับ
|
 |
 |
 |
 |
Date :
2009-12-31 18:01:05 |
By :
nutto54 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable Dt = Session[key] as DataTable;
informationDataContext z = new informationDataContext();
DataSet Ds = new DataSet();
if (Dt == null)
{
Dt = new DataTable();
Ds = new DataSet();
var query = from a in z.jubzs where select a.xml;
var xmlstring = from q in query select q.ToString();
foreach (string h in xmlstring)
{
System.IO.TextReader Tr = new System.IO.StringReader(h);
Ds.ReadXml(Tr);
}
Dt.Columns.Add("Name", typeof(string));
Dt.Columns.Add("City", typeof(string));
Dt.Columns.Add("Age", typeof(string));
Dt = Ds.Tables[0];
}
DataColumn Dc = new DataColumn();
Dc.ColumnName = "ID";
Dc.DataType = System.Type.GetType("System.Int32");
Dt.Columns.Add(Dc);
foreach (DataRow Dr in Dt.Rows)
{
Dr["ID"] = *** YourID ***
}
Session[key] = Dt;
return Dt;
}
|
 |
 |
 |
 |
Date :
2010-01-01 12:38:51 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคับคุณ tungman เดี๋ยวลองทำดูก่อนนะครับ 
|
 |
 |
 |
 |
Date :
2010-01-01 15:21:08 |
By :
nutto54 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|