Click here to Skip to main content
14,692,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my task to create a dynamic table with fixed number of columns
having 2 buttons add and del when i click on add a row to be added below the table.
Posted
Comments
OriginalGriff 5-Jun-12 11:55am
   
And?
What is the problem?
What have you tried?
Where are you stuck?
Clifford Nelson 5-Jun-12 13:34pm
   
Need to know your environment; ASP.NET, WinForms, etc.
Maciej Los 5-Jun-12 15:10pm
   
MS Access? SQL Server?
or
DataTable? DataRow?

What have you tried so far? What code do you have and why isn't it working? What errors are you getting?

It's really hard to help when you haven't started yet and you haven't provided much information. For example, is this a web project or a windows forms or what?

To create a dynamic table, simply declare it. Then add the columns you want. If you need some basic help to get started, try searching on google[^] for some ideas. If you run into a problem come back here and post your code for more help.
   
Comments
Maciej Los 5-Jun-12 15:08pm
   
Is this an answer?
Kschuler 5-Jun-12 15:49pm
   
How can anyone post a real answer without knowing what kind of program is being written? If the google results help, then it's at least a partial answer.
Hello,
check this link , it will be most helpful for creating such program.

DataGridView MSDN Library[^]

If you still need help look at this simple windows form program in C#, that I have made. It consists of one Form with one DataGridView table with two buttons for adding new row with new cell values taken from text boxes ,and one button for deleting last row in the DataGridView.

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 03.03.2012
 * Time: 18:33
 * 
 * Windows form application written in C# 4.0 , needs netframework 4.0
 * 
 * Application for transfering values from text box to DataGridView table
 * 
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Insert_value_from_TextBox__to__DataGridView
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			
			//
			// DataGridView at the begining needs to have at least one row
			// and text boxes needs its initial values
			//
			
			SetInitialValues();
		}
		
		//
		// When user click on button to enter values from text boxes to grid
		//
		void EnterValuesToDatagridView(object sender, EventArgs e)
		{
			// TODO: Implement Button1Click
				
			//
			// Variable for storing new row number in grid
			//
			int Row = 0;
			
			//
			// Check whether the user has entered all the values
			//
			if (textBox1.Text != "" & textBox2.Text != "" & textBox3.Text != "" & textBox4.Text != "")
			{
				
				//
				// Add new row to DataGridView where the values are to be entered
				//
				dataGridView1.Rows.Add();
				//
				// Get Row number ,
				// it is reduced by two because
				// the first row number is zero, after adding new row to allready
				// existing one.
				//
				Row = dataGridView1.Rows.Count - 2;
				
				//
				// Store values from text boxes to DataGridView
				//
				dataGridView1[0,Row].Value = textBox1.Text;
				dataGridView1[1,Row].Value = textBox2.Text;
				dataGridView1[2,Row].Value = textBox3.Text;
				dataGridView1[3,Row].Value = textBox4.Text;
				dataGridView1.Refresh();
				
				//
				// This is optional
				// Clear text boxes
				//
				textBox1.Text = "";
				textBox2.Text = "";
				textBox3.Text = "";
				textBox4.Text = "";
			}
			
			//
			// If all text boxes are not filled in
			//
			else
			{
				MessageBox.Show("You did not entered values to all text boxes","Error",
				                MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		
		//
		// When user click on button to delete last row
		//
		void DeleteLastRow(object sender, EventArgs e)
		{
			// TODO: Implement Button2Click
			
			//
			// Variable for storing last row number in grid
			//
			int LastRow = 0;
			
			LastRow = dataGridView1.Rows.Count - 2;
			
			if (LastRow > -1)
			{
				dataGridView1.Rows.RemoveAt(LastRow);
			}
		}
		
		void SetInitialValues()
		{
			//
			// This is optional
			// Set DataGridView read only property to true
			// User will not be able to enter values to grid directly
			//
			// And allow user to delete rows 
			//
			dataGridView1.ReadOnly = true;
			dataGridView1.AllowUserToDeleteRows = true;
			
			//
			// Set DataGridView number of rows to one , this is necessary
			//
			dataGridView1.RowCount = 1;
			dataGridView1.Refresh();
			
			//
			// Set initial values of text box
			//
			textBox1.Text = "Hemant";
			textBox2.Text = "IT";
			textBox3.Text = "25";
			textBox4.Text = "10 rupee";
		}

	}
}



All the best,
Perić Željko
   
v2

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