开发工具是VS2005,项目是P PC2003移动项目!
问题:控件DataGrid的使用方法!
用代码生成了几行表格!
现在的问题是,运行后表格里面的元素不能改变值!
怎么才能改变表格元素的值?
界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace DeviceApplication2
{
public partial class Form1 : Form
{
private DataTable namesTable;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitTable();
}
private void InitTable()
{
this.namesTable = new DataTable("Names");
this.dataGrid1.DataSource = namesTable;
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "File";
idColumn.AutoIncrement = true;
namesTable.Columns.Add(idColumn);
DataColumn fNameColumn = new DataColumn();
fNameColumn.DataType = System.Type.GetType("System.String");
fNameColumn.ColumnName = "Name";
fNameColumn.DefaultValue = "name";
namesTable.Columns.Add(fNameColumn);
DataColumn numColumn = new DataColumn();
numColumn.DataType = System.Type.GetType("System.Int32");
numColumn.ColumnName = "Data";
numColumn.AutoIncrement = true;
namesTable.Columns.Add(numColumn);
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = this.namesTable.TableName;
int columnNum = 3;
for (int i = 0; i < columnNum; i++)
{
DataGridColumnStyle dgcs = new DataGridTextBoxColumn();
dgcs.Width = (this.dataGrid1.Width - 20 - columnNum) / columnNum;
dgcs.MappingName = this.namesTable.Columns[i].ColumnName;
dgcs.HeaderText = this.namesTable.Columns[i].ColumnName;
dgts.GridColumnStyles.Add(dgcs);
}
this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(dgts);
}
private void button1_Click(object sender, EventArgs e)
{
removeData();
addData();
}
private void button2_Click(object sender, EventArgs e)
{
removeData();
}
//
//addData添加表行
//
public void addData()
{
try
{
int i;
//if (string.IsNullOrEmpty(this.textBox1.Text))
if (int.Parse(textBox1.Text) < 0)
{
MessageBox.Show("Please Input One Number");
return;
}
else
{
for (i = 0; i < int.Parse(this.textBox1.Text); i++)
{
namesTable.Rows.Add(namesTable.NewRow());
}
}
textBox1.Text = "";
this.dataGrid1.DataSource = namesTable;
}
catch (FormatException)
{
MessageBox.Show("Please Input One Number");
}
}
//
//删除所有行falath
//
public void removeData()
{
this.namesTable.Dispose();
InitTable();
this.dataGrid1.DataSource = namesTable;
}
}
}