locked
can not populate data in textbox

Answers

  • Do you mean only one or more of them?

    You can do it this way:

          //creating dataSet and dataTable with 2 columns:
          DataSet ds = new DataSet();
          DataTable table = new DataTable("Mytable1");
          
          //adding dataTable to dataSet:
          ds.Tables.Add(table);
          table.Columns.Add("column1", typeof(int));
          table.Columns.Add("column2", typeof(string));
    
          //filling dataTable with som example data:
          table.Rows.Add(1, "Value 1");
          table.Rows.Add(2, "Value 2");
    
          //populating textBoxes from dataSet:
          textBox1.Text = ds.Tables["MyTable1"].Rows[0]["column1"].ToString();
          textBox2.Text = ds.Tables["MyTable1"].Rows[0]["column2"].ToString();
    
          //this is another way:
          foreach (DataRow dr in ds.Tables["MyTable1"].Rows)
          {
            //this is possible is there is only one row in dataTable
            //if there are more rows, you have to define form which row you want to populate which textBox!
            textBox1.Text = dr[0].ToString();
            textBox2.Text = dr[1].ToString();
          }

    Hope it helps.

    and btw, please change the thread to a question (now its a discusson type).


    Mitja
    Saturday, April 02, 2011 3:59 PM
  • Hi,

    If you want to bind a column to the textbox then navigate over the records then use a binding source and binding navigator with your dataset.

     

    Add a binding navigator along with the textbox

          //Where product dataset is a dataset and Products is a table name
          BindingSource bSource1 = new BindingSource(productsDataSet, "Products");
          //Product name is the column in the table
          textBox3.DataBindings.Add("Text", bSource1, "ProductName");
          bindingNavigator2.BindingSource = bSource1;
    
    Which helps binding a column in the table and navigate

    Wednesday, April 06, 2011 8:36 AM
  • If you wanna populate the data from one table of the DataSet into just one TextBox, you can set the Multiline property of the TextBox to be true, parse the data into a String and assign it to the TextBox, use "\r\n" at the place where you need to jump to a new line.
    Use space or other mechanisms as the separations between columns.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 06, 2011 7:59 AM
    Moderator
  • Lets Start From Scratch....

     

    A Text Can Be Assigned To a Textbox Control by 

    textboxname.Text="Your Text";      //Note This takes String Value.

    If you have something stored in a string varriable that you want to display on Textbox then herz it.

    string yourvarriable="Your Text";

    textboxname.Text=yourvarriable;

    If you want data to be displayed from data set just you need to find a data to be displayed.....

    If you already have a data set lets say ds then

    Lets Take Reference from mitja's Code........    ds.Tables["MyTable1"].Rows[0]["column1"].ToString();

    You need to know From which table you want to extract data from.... Here it is Mytable1

    And you need to know from the column of your table that you want to use for your purpose   Here it is column1

    Or you can use column count also.... As  ds.Tables["MyTable1"].Rows[0][3].ToString(); It will Denote the forth column

    And Finally its Just the row you want display....           Here it is 0     It will denote the first row

        .ToString(); Is used To convert data to string since .Text attribute takes string...... As you Know it...

    You can Use Loops for Manupulating Data to/from dataset.........

    Hope you find out...

     

    Friday, April 08, 2011 10:46 AM

All replies

  • Do you mean only one or more of them?

    You can do it this way:

          //creating dataSet and dataTable with 2 columns:
          DataSet ds = new DataSet();
          DataTable table = new DataTable("Mytable1");
          
          //adding dataTable to dataSet:
          ds.Tables.Add(table);
          table.Columns.Add("column1", typeof(int));
          table.Columns.Add("column2", typeof(string));
    
          //filling dataTable with som example data:
          table.Rows.Add(1, "Value 1");
          table.Rows.Add(2, "Value 2");
    
          //populating textBoxes from dataSet:
          textBox1.Text = ds.Tables["MyTable1"].Rows[0]["column1"].ToString();
          textBox2.Text = ds.Tables["MyTable1"].Rows[0]["column2"].ToString();
    
          //this is another way:
          foreach (DataRow dr in ds.Tables["MyTable1"].Rows)
          {
            //this is possible is there is only one row in dataTable
            //if there are more rows, you have to define form which row you want to populate which textBox!
            textBox1.Text = dr[0].ToString();
            textBox2.Text = dr[1].ToString();
          }

    Hope it helps.

    and btw, please change the thread to a question (now its a discusson type).


    Mitja
    Saturday, April 02, 2011 3:59 PM
  • If you wanna populate the data from one table of the DataSet into just one TextBox, you can set the Multiline property of the TextBox to be true, parse the data into a String and assign it to the TextBox, use "\r\n" at the place where you need to jump to a new line.
    Use space or other mechanisms as the separations between columns.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 06, 2011 7:59 AM
    Moderator
  • Hi,

    If you want to bind a column to the textbox then navigate over the records then use a binding source and binding navigator with your dataset.

     

    Add a binding navigator along with the textbox

          //Where product dataset is a dataset and Products is a table name
          BindingSource bSource1 = new BindingSource(productsDataSet, "Products");
          //Product name is the column in the table
          textBox3.DataBindings.Add("Text", bSource1, "ProductName");
          bindingNavigator2.BindingSource = bSource1;
    
    Which helps binding a column in the table and navigate

    Wednesday, April 06, 2011 8:36 AM
  • Lets Start From Scratch....

     

    A Text Can Be Assigned To a Textbox Control by 

    textboxname.Text="Your Text";      //Note This takes String Value.

    If you have something stored in a string varriable that you want to display on Textbox then herz it.

    string yourvarriable="Your Text";

    textboxname.Text=yourvarriable;

    If you want data to be displayed from data set just you need to find a data to be displayed.....

    If you already have a data set lets say ds then

    Lets Take Reference from mitja's Code........    ds.Tables["MyTable1"].Rows[0]["column1"].ToString();

    You need to know From which table you want to extract data from.... Here it is Mytable1

    And you need to know from the column of your table that you want to use for your purpose   Here it is column1

    Or you can use column count also.... As  ds.Tables["MyTable1"].Rows[0][3].ToString(); It will Denote the forth column

    And Finally its Just the row you want display....           Here it is 0     It will denote the first row

        .ToString(); Is used To convert data to string since .Text attribute takes string...... As you Know it...

    You can Use Loops for Manupulating Data to/from dataset.........

    Hope you find out...

     

    Friday, April 08, 2011 10:46 AM