Click here to Skip to main content
15,942,799 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hey Guys,

I need a table where the User can edit cells, like in Microsoft Excel. I need ~3x20 blank Cells where the User can Add his Data. The User should click on the Cell and edit the Cell by using his keyboard.
Is a DataGrid the right thing for doing this? And How can I realize this?

Thanks for your help!
Posted

Hello Brother,
Quote:
The User should click on the Cell and edit the Cell by using his keyboard.
Is a DataGrid the right thing for doing this? And How can I realize this?

You can do this using TablelayoutPanelControl. I feel it is much more flexible(Please correct me if i am wrong).
In a tablelayoutpanel control you can add controls you like to generate a template. I can even generate a form with textboxes and buttons.
In your case you want to generate a table of 3 rows and 20 columns so:

On Form Load:
private void Form1_Load(object sender, EventArgs e)
       {
           tableLayoutPanel1.ColumnCount = 20; ////start with a lower value say 5, because you will have to expand your tablelayoutpanel control by dragging its boundries for bigger value
           tableLayoutPanel1.RowCount = 3;
           for (int i = 0; i < 20; i++)
           {
               for (int j = 0; j < 3; j++)
               {
                   tableLayoutPanel1.Controls.Add(new TextBox(), i, j);
               }

           }
       }

- But please note when you add a tablelayoutpanel control to the form, make sure that you "autosize" both its rows and columns. Because by default the tablelayoutpanel control only renders 2 columns.
- To do that you need to right click on the tablelayoutpanel select the rows option and set the size mode to autosize. Similarly you need to do that for columns also.
- Again recheck if the Autosize for Rows and Columns has been set or not. If not, do it again.
- If the number of rows and columns increase then while rendering the application may reach into a non responsive state. So you need to use a background worker for that. I have just suggested you a control and the logic.
- Also its a very wrong thing to perform any action on FormLoad (which i have done here)best is to initialize a thread in the background, that thread will render the control on the form. Use backgroundworker. Please read about background worker in depth. Its very helpful when you want to render many things on UI amd perform any operation also.

Refer:
BackgroundWorker Class Sample for Beginners[^]

And please dont mind: A small homework: Try to make this application dynamic. Accept the row and column count from textboxes :)
Please do tell me whether the code i posted works or not?

Thanks,
- Rahul
 
Share this answer
 
v4
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900