Friday, November 27, 2009

How to display FlexGrid data to xml and viceversa

private void _btnLoadData_Click(object sender, System.EventArgs e)
{
string mdbfile = @"c:\program files\componentone studio.NET 2.0\Common\nwind.mdb";
string conn = @"provider=microsoft.jet.oledb.4.0;data source=" + mdbfile + ";";
string sql = "select * from customers";
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, conn);
//DataTable dt = new DataTable();
da.Fill(dt);
_flex.DataSource = dt;

string sql1 = "select * from Employees";
System.Data.OleDb.OleDbDataAdapter da1 = new System.Data.OleDb.OleDbDataAdapter(sql1,conn);
DataTable dt_Emp = new DataTable();
da1.Fill(dt_Emp);
c1FlexGrid1.DataSource = dt_Emp;
string sql2 = "select * from Products ";
System.Data.OleDb.OleDbDataAdapter da_Prod = new System.Data.OleDb.OleDbDataAdapter(sql2,conn);
DataTable dt_Prod = new DataTable();
da_Prod.Fill(dt_Prod);
_flex_Products.DataSource = dt_Prod;
}
// save to a single-sheet book
private void _btnSaveSheet_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt1 = (DataTable)_flex.DataSource;
ds.Tables.Add(dt1);
ds.WriteXml("XMLFileOut.xml", XmlWriteMode.IgnoreSchema);
DataSet ds_Empx = new DataSet();
DataTable dt_EmpX = (DataTable)c1FlexGrid1.DataSource;
ds_Empx.Tables.Add(dt_EmpX);
ds_Empx.WriteXml("XMLEmpFile.xml",XmlWriteMode.IgnoreSchema);
DataSet ds_Prod = new DataSet();
DataTable dt_Prod = (DataTable)_flex_Products.DataSource;
ds_Prod.Tables.Add(dt_Prod);
ds_Prod.WriteXml("Product_Details.xml",XmlWriteMode.IgnoreSchema);
}

private void _btnLoadSheet_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string filePath = @"XMLFileOut.xml";
ds.ReadXml(filePath);
_flex.DataSource = ds;
_flex.DataMember = "Table1";

DataSet ds_Emp = new DataSet();
string file_Emp = @"XMLEmpFile.xml";
ds_Emp.ReadXml(file_Emp);
c1FlexGrid1.DataSource = ds_Emp;
c1FlexGrid1.DataMember = "Table1";

DataSet ds_Prod = new DataSet();
string file_Prod = @"Product_Details.xml";
ds_Prod.ReadXml(file_Prod);
_flex_Products.DataSource = ds_Prod;
_flex_Products.DataMember = "Table1";
}
}

No comments:

Post a Comment