your btn_click function. Every loop will initialize or create a new object inside the aaa(i) function
Every time aaa(i) is called in the for loop DataTable dt = new DataTable() will be called
public void aaa(int i)
{
DataTable dt = new DataTable(); ///this will initialize every time, a new data table will be created every loop
dt.Columns.Add("host");
DataRow dr = dt.NewRow();
for (int a = 1; a <= i; a++)
{
dr[a] = i;
}
dt.Rows.Add(dr);
this.dataGridView1.DataSource = dt;
}
}
Might i suggest you pass the 254 int in your aaa(i) function and do the loop inside like
private void button1_Click(object sender, EventArgs e)
{
aaa(254);
}
public void aaa(int i) //value of i = 254
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
for (var s = 0; s <= i; s++ ) {
for (int a = 1; a <= i; a++)
{
dr[a] = i;
}
dt.Rows.Add(dr);
this.dataGridView1.DataSource = dt;
}
}
}
or if the reason for the loop in the button is just for the number limit in the loop inside void aaa then you can simplify it as
public void aaa(int i) //value of i = 254
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
for (int a = 1; a <= i; a++)
{
dr[a] = i;
}
dt.Rows.Add(dr);
this.dataGridView1.DataSource = dt;
}
}