我现在想做一个应用软件 作用是点击按钮后 随机生成一个ID 和密码 ,并放在DATAGRID里 可是我写完后 每次点一下按钮 所有的行都满了 请高手帮帮我
private void button1_Click(object sender, System.EventArgs e)
{
string randomString = null;
Random rNumber = new Random(Environment.TickCount);
for(int i = 0; i < 10; i++)
{ randomString = randomString + Convert.ToChar(rNumber.Next(65,90)); }
textBox1.Text = randomString;
string randomString1 = null;
for(int i = 0; i < 10; i++)
{ randomString1 = randomString1 + Convert.ToChar(rNumber.Next(65,90)); }
textBox2.Text = randomString1;
DataTable dt = new DataTable("dt");
DataColumn dc1 = new DataColumn("ID" ,Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("Password",Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("代理商",Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow r;
for(int i = 0;i<5; i++)
{
// RowChanged event will occur for every addition.
r= dt.NewRow();
r[0]= i;
dt.Rows.Add(r);
}
dt.AcceptChanges();
int j = 0;
dt.AcceptChanges();
dt.Rows[0].BeginEdit();
dt.Rows[1].BeginEdit();
dt.Rows[j][0]= textBox1.Text.Trim();
dt.Rows[j][1]= textBox2.Text.Trim();
j++;
try
{
dt.Rows[0].EndEdit();
dt.Rows[1].EndEdit();
}
catch(Exception a)
{
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = "My Application";
log.WriteEntry(a.ToString());
Console.WriteLine("Exception of type {0} occurred.", a.GetType());
}
this.dataGrid1.DataSource = dt;
}
private void button1_Click(object sender, System.EventArgs e)
{
string randomString = null;
Random rNumber = new Random(Environment.TickCount);
for(int i = 0; i < 10; i++)
{ randomString = randomString + Convert.ToChar(rNumber.Next(65,90)); }
textBox1.Text = randomString;
string randomString1 = null;
for(int i = 0; i < 10; i++)
{ randomString1 = randomString1 + Convert.ToChar(rNumber.Next(65,90)); }
textBox2.Text = randomString1;
DataTable dt = new DataTable("dt");
DataColumn dc1 = new DataColumn("ID" ,Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("Password",Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("代理商",Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow r;
for(int i = 0;i<5; i++)
{
// RowChanged event will occur for every addition.
r= dt.NewRow();
r[0]= i;
dt.Rows.Add(r);
}
dt.AcceptChanges();
int j = 0;
dt.AcceptChanges();
dt.Rows[0].BeginEdit();
dt.Rows[1].BeginEdit();
dt.Rows[j][0]= textBox1.Text.Trim();
dt.Rows[j][1]= textBox2.Text.Trim();
j++;
try
{
dt.Rows[0].EndEdit();
dt.Rows[1].EndEdit();
}
catch(Exception a)
{
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = "My Application";
log.WriteEntry(a.ToString());
Console.WriteLine("Exception of type {0} occurred.", a.GetType());
}
this.dataGrid1.DataSource = dt;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货