textbox3应该使用手机号码(12位数字以允许国家/地区代码),然后在datagridview的第2行填充它。第一行是我编码的反向序列号。
我尝试了各种方法。
public char[] c;
public void addMobile()
{
//char[] i = c;
List<string> columnheader = new List<string>();
columnheader.Add("12");
columnheader.Add("11");
columnheader.Add("10");
columnheader.Add("9");
columnheader.Add("8");
columnheader.Add("7");
columnheader.Add("6");
columnheader.Add("5");
columnheader.Add("4");
columnheader.Add("3");
columnheader.Add("2");
columnheader.Add("1");
/*
dataGridView1.ColumnCount = 12;
dataGridView1.DataSource = columnheader;
*/
DataGridViewColumn newColumn;
//dataGridView1.ColumnCount = 12;
foreach (var item in columnheader)
{
newColumn = new DataGridViewColumn();
newColumn.HeaderText = item;
newColumn.CellTemplate = new DataGridViewTextBoxCell(); ;
dataGridView1.Columns.Add(newColumn);
}
DataGridViewRow dgvRow;
try
{
for (int temp = 0; temp < c.Length; temp++)
{
string[] data = new string[12];
data[temp] = c[temp].ToString();
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[temp].Value = data[temp];
dataGridView1.Rows.Add();
}
}
catch(Exception e)
{
}
//int row;
/*
for (var row = 0; row < c.Length; row++)
{
dgvRow = new DataGridViewRow();
dgvRow.Cells[row].Value = c[row];
dataGridView1.Rows.Add(dgvRow);
}
*/
/*
try
{
foreach (var item in c)
{
dgvRow = new DataGridViewRow();
dgvRow.SetValues(item.ToString());
dataGridView1.Rows.Add(dgvRow);
}
}
catch (Exception e)
{
}
* */
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox3.Text, "[^0-9]"))
{
MessageBox.Show("Please enter only numbers.");
textBox3.Text.Remove(textBox3.Text.Length - 1);
c = textBox3.Text.ToCharArray();
}
}
private void button1_Click(object sender, EventArgs e)
{
addMobile();
//MessageBox.Show("Your Star Sign is " + s);
//PatternMatch(c);
}
我添加了相关的代码段。评论部分是我尝试的各种方式或者相当混乱。
答案 0 :(得分:0)
这里发生了几个问题。
问题1修复
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox3.Text, "[^0-9]"))
{
MessageBox.Show("Please enter only numbers.");
textBox3.Text.Remove(textBox3.Text.Length - 1);
}
else // Only numbers were entered. Fill the char array.
{
c = textBox3.Text.ToCharArray();
}
}
第2期&amp; 3修复
public void addMobile()
{
List<string> columnheader = new List<string>();
for (int i = 12; i > 0; i--)
{
columnheader.Add(i.ToString());
}
DataGridViewColumn newColumn;
foreach (var item in columnheader)
{
newColumn = new DataGridViewColumn();
newColumn.Name = item; // Give the column its unique name.
newColumn.HeaderText = item;
newColumn.CellTemplate = new DataGridViewTextBoxCell();
// Only add the column if a column of the same name doesn't already exist.
// Prevents duplicates.
if (!dataGridView1.Columns.Contains(newColumn.Name))
{
dataGridView1.Columns.Add(newColumn);
}
}
DataGridViewRow dgvRow;
dgvRow = new DataGridViewRow(); // New row - has no cells.
dgvRow.CreateCells(dataGridView1); // Create cells matching the dgv columns.
for (var row = 0; row < c.Length; row++)
{
dgvRow.Cells[row].Value = c[row];
}
// Add the rows outside of the forloop.
// Otherwise you'll get a new row containing only one value, for each of the 12 digits.
dataGridView1.Rows.Add(dgvRow);
}
额外修复
注意:如果用户输入的数字超过12位,您将遇到问题。你应该处理。例如:
private void button1_Click_1(object sender, EventArgs e)
{
if (c.Length != 12)
{
MessageBox.Show("Number isn't 12 digits.");
}
else
{
addMobile();
}
}
旁注:我不确定您为什么要将每个数字添加到单独的列中。带有格式掩码的单个电话号码列将更具可读性和实用性。