How to dynamically Create huge grid of editable textboxes at runtime..Help..

I'm working on a windows program that will open a tsv (tab seperated file) and store the values into an arrary and then put these values into a grid of textboxes where I can edit the values.

I got to the point where i parse the text files into the arrary fine and I can put the values into the grid of textboxes.. but Im creating a grid of 12 X 100 which is 12000 textboxes i manually have to put into the form through the IDE. There has to be a better way to do this at runtime.

Is there a way to create the textboxes at runtime where I can put it into a loop.

for example:

txtbox1...txtbox2, txtbox 3 and so on......

i want to store my arrary values into them.

ex. txtbox1.Text = aryMyarray[0];

txtbox2.Text = aryMyarray[1];.........

Im sure theres a way tot do this. If someone out there has an idea please let me know Thanks ahead! Feel free to email me.

I'm new to C# and just scratching the surface so far im loving it. IM working on this application at work to automate some of the tedious tasks. At the same time I get to experiement with C#




Answer this question

How to dynamically Create huge grid of editable textboxes at runtime..Help..

  • Ljungstedt

    So instead of puting my TSV file data into a 1 dimensional array I should put into a 2 dimensional arrary . That way when I bind my data grid to my arrary it will work b/c data grids are like 2D. Ill try out that code as soon as I get a chance. If not later today, I can do it at work tommorow.

    You guys are great, I'm learning alot from here.



  • a.stalmokas

    hi,

    yes you can write it back to textfile or you can write it to xml file which will be easier because you will write less code like mydataset.readxml("myflile.xml") but in the text form you have to provide a pattern for that

    the best way to do with your data is to put a datasource in your form and to make it between your datagridview and your datatable it will update the values itself for you because reaching to a particular cell in datagridview is a little bit silly you have to get it by its coordinate like column number and row number something like this

    this.MyDataGridView.Rows[0].Cells[0].Selected = false;

    so add a dataset , and binding source to you form designer

    after the creation of your table add it to your dataset

    mydataset.tables.add(mytable)

    relate you datasource to the database

    mybindingsource.datasource = mydataset

    mybindingsource.datamemeber = "mytablename"

    binding your datagridview to your datasource

    mydatagridview.datasource = mybidingsource

    now every change you gonna made is saved back to your table b4 you exist your program chose the way you want to save it xml of text



  • Sam Dela Cruz

    That code works beautifully shakalama. It looks more professional too. I like to sort feature. I was able to load my TSV file into the datagrid.

    I'm just worried about one thing. The data that will be stored in this datagrid comes from a TSV file. But I wanted to be able to update the data from another source if needed.

    It seemed pretty easy if I had a textbox grid because, every textbox had its own name so I can easily update any paticular textbox field in the grid. How can I do this with the datagrid

    say if I wanted to update a field in this location row 5, column 7 or row 8, column 9. Can this be done, I would think the sort feature would change these locations each time I sorted it, so should I disable the sort feature to achieve this

    Thanks again....

    Chan



  • Jan Ku?era

    Hi!

    Why not use DataGridView and dynamically construct columns



  • Otto Martinez

    My application calls for many textboxes due to the interface.  How do i get textboxes to point to a specific database value.  I am using vb2005.
  • cristianv

    hi,

    i guess the best thing to do is to use the datagridview instead of creating all those textbox something like that

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)

    {

    string [,] mystring = new string[2,2];

    mystring[0,0] = "haitham";

    mystring[0,1] = "nabil";

    mystring[1,0] = "ehab";

    mystring[1,1] = "lotfy";

    //creat a table to store the values in it

    DataTable name = new DataTable("name");

    name.Columns.Add("personal");

    name.Columns.Add("parents");

    for (int row = 0; row < mystring.GetLength(1); row++)

    {

    //looping through my array geting hte value storeing it

    //into my table

    DataRow dr = name.NewRow();

    dr["personal"] = mystring[row,0];

    dr["parents"] = mystring[row, 1];

    name.Rows.Add(dr);

    }

    //binding my data to datagridview

    dataGridView1.DataSource = name;

    dataGridView1.ReadOnly = true;

    }

    if you are insist to use the textboxs you can use this but you have to make sure of your form height property

    //general variables

    private string[] myname ={ "haitham", "nabile", "Mohamed", "salem", "ismael", "khatab", "ali", "ibrahim"};

    private Point textboxlocation = new Point(10, 10);

     

    private void Form2_Load(object sender, EventArgs e)

    {

    for (int i = 0 ; i < myname.Length; i++)

    {

    TextBox mytextbox = new TextBox() ;

    mytextbox.Name = "txtbx" + i;

    mytextbox.Text = myname [ i ];

    mytextbox.Location = new Point(textboxlocation.X , textboxlocation.Y);

    textboxlocation.Y += +mytextbox.Height + 5;

    Controls.Add(mytextbox);

    }

    hope that helps



  • Matt Maddin

    I agree with Sergey, your life would be much easier if you used a DataGrid... if you absolutely must use TextBoxes though... in code, create an array of TextBoxes and populate it not unlike this...

                for (int x = 0; x < DesiredNumberOfTextBoxes; x++)
                {
                    textBoxes[x] = new TextBox();
                    textBoxes[x].Location = //Set desired location
                    textBoxes[x].Size = //Set desired size
                    Controls.Add(textBoxes[x]); //Add TextBox to parent container
                }

    Only a bit bigger and probably using a two dimensional array and more advanced math for positioning.



  • Adam Goossens

    But if you can - don't use so many text boxes. They all will require underlying Windows EDIT control, they all eat a lot of system resources! And up/down navigation will not work natively...

  • jerio

    Why didn't I think of that.. maybe its because I've never used one before......learn something everything. Ill give it a shot....as long as the end user will be able to edit the values inside the datagrid...this should work perfectly.

    Thanks alot for your help. Im new to this forum and I'm rather surprised at how fast I got an answer. Appreciate it.



  • Robbertos

     Chan Inthisone wrote:

    So instead of puting my TSV file data into a 1 dimensional array I should put into a 2 dimensional arrary .   That way when I bind my data grid to my arrary it will work b/c data grids are like 2D.   Ill try out that code as soon as I get a chance. If not later today, I can do it at work tommorow.   

    You guys are great, I'm learning alot from here.

    no , this was a part from something i was doing here and my example was 2 D array you can make it 1D, but i wasn't able to bind it to the datagrid directly(regardless 1D or 2D) so i created a table from it first then i bound the table tomy datagridview

    if you are aray is 1D thats fine go with it , its its 2 or three you can do it depending on how many column you want to see in your datagridview

    hope that helps



  • How to dynamically Create huge grid of editable textboxes at runtime..Help..