none
How to bind data grid view from xml database file based on search ? RRS feed

  • Question

  • Hi all,

              I am trying to make Document uploader winforms c# application ,where we can upload important documents by name  and then search it by name so that can open the document and if required can print the document ,i am using xml database file as database so i am getting problem in binding the datagridview based on search from textbox using Like operator as in sql , and i am also getting the problem on how to make link in data gridview to open the document and make link in datagridview to print the document ? Below is my xml file ,please help in this i am stuck from many days ? Below is the xml file .

    The document uploaded is kept in the folder named "Doc_Upl".

    I am using VS 2010 and not working in MVC framework.

    <?xml version="1.0" standalone="yes"?>
    <DOCUMENT_UPLOADER>
      <DOCUMENT>
        <OWNER_ID>Rohit Johri</OWNER_ID>
        <DOCUMENT_NAME>xczb</DOCUMENT_NAME>
        <DOCUMENT_URL>1372016_152710images.jpe</DOCUMENT_URL>
      </DOCUMENT>
      <DOCUMENT>
        <OWNER_ID>Rohit Johri</OWNER_ID>
        <DOCUMENT_NAME>xczb</DOCUMENT_NAME>
        <DOCUMENT_URL>1372016_152710images.jpe</DOCUMENT_URL>
      </DOCUMENT>
      <DOCUMENT>
        <OWNER_ID>Rohit Johri</OWNER_ID>
        <DOCUMENT_NAME>zx</DOCUMENT_NAME>
        <DOCUMENT_URL>1372016_153247Student.jpg</DOCUMENT_URL>
      </DOCUMENT>
      <DOCUMENT>
        <OWNER_ID>Rohit Johri</OWNER_ID>
        <DOCUMENT_NAME>asss</DOCUMENT_NAME>
        <DOCUMENT_URL>1372016_153530download.jpe</DOCUMENT_URL>
      </DOCUMENT>

    Thanks in advance

    Rohit J



    Tuesday, July 26, 2016 6:55 AM

Answers

  • Hi Rohit J,

    >>"i am getting problem in binding the datagridview based on search from textbox using Like operator as in sql , "

    I suggest you use Linq-XML to query the data from XML file. Firstly, you need to define a class.

    public class DOCUMENT
    {
        public string OWNER_ID { get; set; }
        public string DOCUMENT_NAME { get; set; }
        public string DOCUMENT_URL { get; set; }
    }

    Then we will query the data from xml and get a collection of DOCUMENT. The contains method is equals to 'like' in SQL.

    public List<DOCUMENT> QueryXml(string keyword)
    {
        string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "\\Doc_Upl\\DOCUMENT_UPLOADER.xml";
        XDocument document = XDocument.Load(xmlPath);
        var documents = document.Root.Elements()
            .Where(s => s.Element("DOCUMENT_NAME").Value.Contains(keyword))
            .Select(s => new DOCUMENT
            {
                OWNER_ID = s.Element("OWNER_ID").Value,
                DOCUMENT_NAME = s.Element("DOCUMENT_NAME").Value,
                DOCUMENT_URL = s.Element("DOCUMENT_URL").Value
            }).ToList();
        return documents;
    }

    >>"and i am also getting the problem on how to make link in data gridview to open the document and make link in datagridview to print the document ? "

    You could use DataGridLinkColumn to show the data for DOCUMENT_URL and handle dataGridView1_CellClick event to do the print operation.

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            object cellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            //do anything you wanto
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 28, 2016 3:12 AM
  • Hi Rohit J,

    >>Inconsistent accessibility: return type 'System.Collections.Generic.List<Documents_Uploader.DOCUMENT>' is less accessible than method 'Documents_Uploader.Form1.QueryXml(string)'

    I think you missed public keyword when you defined DOCUMENT class. Please add it back.

    public class DOCUMENT
    {
        public string OWNER_ID { get; set; }
        public string DOCUMENT_NAME { get; set; }
        public string DOCUMENT_URL { get; set; }
    }

    >>"what to do if i want to search by both QWNER_ID and DOCUMENT_NAME "

    You could change the where statement block to add new condition.

    public List<DOCUMENT> QueryXml(string keyword)
    {
        string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "\\Doc_Upl\\DOCUMENT_UPLOADER.xml";
        XDocument document = XDocument.Load(xmlPath);
        var documents = document.Root.Elements()
            .Where(s => s.Element("DOCUMENT_NAME").Value.Contains(keyword) ||
                s.Element("OWNER_ID").Value.Contains(keyword))
            .Select(s => new DOCUMENT
            {
                OWNER_ID = s.Element("OWNER_ID").Value,
                DOCUMENT_NAME = s.Element("DOCUMENT_NAME").Value,
                DOCUMENT_URL = s.Element("DOCUMENT_URL").Value
            }).ToList();
        return documents;
    }

    >>"and then how to bind Datagridview with it "

    That is easy. You just need to get the return value of QueryXml and set it as the data source of DataGridView control.

    List<DOCUMENT> documents = QueryXml(txtKeyword.Text);
    dataGridView1.DataSource = documents;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 29, 2016 5:49 AM

All replies

  • Hi Rohit J,

    >>"i am getting problem in binding the datagridview based on search from textbox using Like operator as in sql , "

    I suggest you use Linq-XML to query the data from XML file. Firstly, you need to define a class.

    public class DOCUMENT
    {
        public string OWNER_ID { get; set; }
        public string DOCUMENT_NAME { get; set; }
        public string DOCUMENT_URL { get; set; }
    }

    Then we will query the data from xml and get a collection of DOCUMENT. The contains method is equals to 'like' in SQL.

    public List<DOCUMENT> QueryXml(string keyword)
    {
        string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "\\Doc_Upl\\DOCUMENT_UPLOADER.xml";
        XDocument document = XDocument.Load(xmlPath);
        var documents = document.Root.Elements()
            .Where(s => s.Element("DOCUMENT_NAME").Value.Contains(keyword))
            .Select(s => new DOCUMENT
            {
                OWNER_ID = s.Element("OWNER_ID").Value,
                DOCUMENT_NAME = s.Element("DOCUMENT_NAME").Value,
                DOCUMENT_URL = s.Element("DOCUMENT_URL").Value
            }).ToList();
        return documents;
    }

    >>"and i am also getting the problem on how to make link in data gridview to open the document and make link in datagridview to print the document ? "

    You could use DataGridLinkColumn to show the data for DOCUMENT_URL and handle dataGridView1_CellClick event to do the print operation.

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            object cellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            //do anything you wanto
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 28, 2016 3:12 AM
  • Hi ,

          Error coming as:-

    Error 1 Inconsistent accessibility: return type 'System.Collections.Generic.List<Documents_Uploader.DOCUMENT>' is less accessible than method 'Documents_Uploader.Form1.QueryXml(string)' C:\Users\hp\Documents\Visual Studio 2010\Projects\Documents Uploader\Documents Uploader\Form1.cs 103 31 Documents Uploader

    Please help me to resolve this and what to do if i want to search by both QWNER_ID and DOCUMENT_NAME , and then how to bind Datagridview with it ,i am stuck.

    Thanks

    Rohit J


    Thursday, July 28, 2016 3:54 PM
  • Hi Rohit J,

    >>Inconsistent accessibility: return type 'System.Collections.Generic.List<Documents_Uploader.DOCUMENT>' is less accessible than method 'Documents_Uploader.Form1.QueryXml(string)'

    I think you missed public keyword when you defined DOCUMENT class. Please add it back.

    public class DOCUMENT
    {
        public string OWNER_ID { get; set; }
        public string DOCUMENT_NAME { get; set; }
        public string DOCUMENT_URL { get; set; }
    }

    >>"what to do if i want to search by both QWNER_ID and DOCUMENT_NAME "

    You could change the where statement block to add new condition.

    public List<DOCUMENT> QueryXml(string keyword)
    {
        string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "\\Doc_Upl\\DOCUMENT_UPLOADER.xml";
        XDocument document = XDocument.Load(xmlPath);
        var documents = document.Root.Elements()
            .Where(s => s.Element("DOCUMENT_NAME").Value.Contains(keyword) ||
                s.Element("OWNER_ID").Value.Contains(keyword))
            .Select(s => new DOCUMENT
            {
                OWNER_ID = s.Element("OWNER_ID").Value,
                DOCUMENT_NAME = s.Element("DOCUMENT_NAME").Value,
                DOCUMENT_URL = s.Element("DOCUMENT_URL").Value
            }).ToList();
        return documents;
    }

    >>"and then how to bind Datagridview with it "

    That is easy. You just need to get the return value of QueryXml and set it as the data source of DataGridView control.

    List<DOCUMENT> documents = QueryXml(txtKeyword.Text);
    dataGridView1.DataSource = documents;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 29, 2016 5:49 AM