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

问题描述

这是我的任务,以创建具有固定列数的动态表
当我单击要在表格下方添加的行时,有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.

推荐答案


/*
 * 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


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

08-21 04:31