如何创建动态表 [英] how to create dynamic table

查看:83
本文介绍了如何创建动态表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的任务,以创建具有固定列数的动态表
当我单击要在表格下方添加的行时,有2个按钮添加和删除.

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.

推荐答案

您到目前为止尝试了什么?您有什么代码,为什么不起作用?您遇到什么错误?

当您还没有开始并且没有提供太多信息时,真的很难提供帮助.例如,这是Web项目还是Windows窗体?是什么?

要创建动态表,只需声明它即可.然后添加所需的列.如果您需要入门方面的基本帮助,请尝试在
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.


您好,
检查此链接,它将对创建此类程序最有帮助.

DataGridView MSDN库 [
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";
		}

	}
}




祝一切顺利,
PerićŽeljko




All the best,
Perić Željko


这篇关于如何创建动态表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆