Update Textbox Based On Value Selected In Combobox

Answered Update Textbox Based On Value Selected In Combobox

  • Friday, November 25, 2011 5:55 PM
     
     

    Hi,

    I have combox with 4 values displayed from the XML node on windows form. I want to update XML from textbox on same form based on value elected in combobox. so if 1st value  got selected in combobox, its other node value should be shown on textbox. How to do it..

    for ex:  i have a XML file like

    <Products>

       <Prod>

        <No>1</No>

        <Name>APPLE</Name>

      </Prod>

      <Prod>

        <No>2</No>

        <Name>ORANGE</Name>

      </Prod>

    </Products>

    If i select the number 1 from combo box list, the APPLE should be shown on text box automatically and if i select 2 from combo box it should show ORANGE on text box. 

    Please help me.....

    Thank u in advance...



    • Edited by NastyFreak Friday, November 25, 2011 6:02 PM
    •  

All Replies

  • Sunday, November 27, 2011 3:41 PM
     
     Proposed Has Code

    hi

    this solution should help you

    dim valuecombo as string
    valuecombo=combobox1.selectedvalue
    Dim ds As New DataSet
    Dim strPathXML As String
    strPathXML = Path.GetFullPath(Filexml)
      If (File.Exists(strPathXML)) Then
                ds.ReadXml(Filexml)
                End If
    if valuecombo="1" then 
    
    
            Textbox.Text = ds.Tables("Products").Rows(0)("Name").ToString
    
    else 
            Textbox.Text = ds.Tables("Products").Rows(1)("Name").ToString
    end if
    
    Regards


    Best Regards...Please mark as answer if my post is helpful
  • Monday, November 28, 2011 3:41 AM
    Moderator
     
     Answered Has Code

    Hi NastyFreak,
    Welcome to the MSDN Forum.
    Another way to do this is using Linq to Xml. The code below could help you.

    XDocument xdoc = new XDocument();
    string filePath = @"E:\Private\TestFile\Products.xml"; // replace the path
    private void Form1_Load(object sender, EventArgs e)
    {
        xdoc = XDocument.Load(filePath);
        var noList = xdoc.Root.Descendants("Prod").Elements("No").Select(x => x.Value).ToList();
    
        foreach (var no in noList)
        {
            comboBox1.Items.Add(no);
        }
    }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s = this.comboBox1.SelectedItem as string;
    
        if (s != null)
        {
            var name = (from xNode in xdoc.Root.Descendants("Prod") where xNode.Element("No").Value == s select xNode.Element("Name").Value).FirstOrDefault();
    
            if (name != null)
            {
                this.textBox1.Text = name;
            }
        }
    }
    


    In this code, the code get all No’s values in the Form1_load method and get related name according to the No selected.
    For more information about Linq to Xml, please check this document.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked As Answer by NastyFreak Wednesday, November 30, 2011 10:39 AM
    •  
  • Wednesday, November 30, 2011 10:38 AM
     
     

    Thanks you...

     

  • Wednesday, November 30, 2011 10:40 AM
     
     
    Thank You
  • Thursday, December 01, 2011 2:19 AM
    Moderator
     
     
    You’re welcome.
    If there is anything unclear, please let us know.
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
  • Thursday, December 01, 2011 1:31 PM
     
     

    Hi 

    I have 4 Text Box and 1 Button.. When i enter value in TextBox and click the button.

    My datas should bind in DataGrid View. And after inserting in Datagrid. When i double click the data row

    from DataGrid, the values should be again placed in textboxes so that i can edit data and update the same row. How to do it..

     

    Please Help Me....

    Thanks in advance....

  • Friday, December 02, 2011 5:51 AM
    Moderator
     
      Has Code

    Hi NastyFreak,
    You can use DataTable to store the data and CellMouseDoubleClick event  to return the value to TextBox, try the code below.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        DataTable dt = new DataTable();
        List<TextBox> tbList = new List<TextBox>();
        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("Col_A");
    
            tbList.Add(textBox1);
            tbList.Add(textBox2);
            tbList.Add(textBox3);
            tbList.Add(textBox4);
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            dt.Rows.Clear();
            foreach (TextBox tb in tbList)
            {
                dt.Rows.Add(tb.Text);
            }
            dataGridView1.DataSource = dt;
        }
    
        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            tbList[e.RowIndex].Text = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string;
        }
    }
    

    However, why not change the data in datagridview immediately?
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us