简体   繁体   English

如何将C#中的Datagridview与xml对象绑定

[英]How to bind datagridview in c# with xml object

I am working on a winform application that use amazon web service. 我正在使用Amazon Web Service的Winform应用程序上工作。 I have made request to get data, the response is in xml object, But i dont know how to bind this xml response with datagridwiew, here is my code.. 我已经请求获取数据,响应是在xml对象中,但是我不知道如何用datagridwiew绑定此xml响应,这是我的代码。

 private void btnRun_Click(object sender, EventArgs e)
        {
            if (CheckForInternetConnection())
                try
                {
                    // Instantiate Amazon ProductAdvertisingAPI client
                    amazonClient = new AWSECommerceServicePortTypeClient();

                    // prepare an ItemSearch request
                    request = new ItemSearchRequest();

                    request.SearchIndex = "Books";
                    request.Title = "The Life and Love of the Sea";
                    request.ResponseGroup = new string[] { "Small" };

                    itemSearch = new ItemSearch();
                    itemSearch.Request = new ItemSearchRequest[] { request };
                    itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
                    itemSearch.AssociateTag = "AssociationTag";

                    // send the ItemSearch request
                    response = amazonClient.ItemSearch(itemSearch);
                    // here i want to bind the datagridview with this xml response.

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            else
                MessageBox.Show("No Internet Connection found");
        }

First you need to create a DataTable from your xml data and then bind the DataTable to your gridView. 首先,您需要根据xml数据创建一个DataTable,然后将DataTable绑定到gridView。

This is the code that i use to create a DataTable from an xml file : 这是我用来从xml文件创建DataTable的代码:

//This method return a dataTable created with your xml data
 public static DataTable  getDataByRessource()
    {
        //Create the new xml document
        XmlDocument xmlDoc = new XmlDocument();

        //Here you can load your file , i load it with the resources properties of my project
        xmlDoc.LoadXml(Properties.Resources.TRADUCTION);
        //This dataSet will contains your dataTable with your data
        DataSet ds = new DataSet();
         //Parse the xml to a dataTable
        XmlNodeReader xnr = new XmlNodeReader(xmlDoc);
        ds.ReadXml(xnr);

        //return your dataTable
        return  ds.Tables[0];
    }

Now you only have to bind the DataSource of your gridView to this DataTable. 现在,您只需要将gridView的DataSource绑定到此DataTable。

EDIT : 编辑:

Take a look here, it's probably what you want to achieve : http://www.midniteblog.com/?p=20 在这里看看,这可能是您想要实现的目标: http : //www.midniteblog.com/?p=20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM