我的datagridview中有一个表,我以相同的形式动态创建了文本框。 我想知道我的列数和行数,但其值始终显示为0
int j = dmrc.dataGridView2.Rows.Count;
int k = dmrc.dataGridView2.ColumnCount;
for (int i = 0; i<=j+2; i++)
{
TextBox txtRun = new TextBox();
txtRun.Name = "tb"+i;
txtRun.Location = new System.Drawing.Point(20 , 18 +(25*i));
txtRun.Size = new System.Drawing.Size(100, 25);
this.Controls.Add(txtRun);
}
for (int i = 0; i <= j+2; i++)
{
TextBox txtRun = new TextBox();
txtRun.Name = "tb" +i;
txtRun.Location = new System.Drawing.Point(150, 18 + (25* i));
txtRun.Size = new System.Drawing.Size(100, 25);
txtRun.Text = Convert.ToString(j);
this.Controls.Add(txtRun);
}
在此之前,我已经进行了OleDB连接以进口EXCEL文件
试 {
DataGridView dataGridview2 = new DataGridView();
dmrc dmrc = new dmrc();
string pathconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path.Text + "; Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(pathconn);
OleDbDataAdapter mydata = new OleDbDataAdapter("Select * from [" + sheet.Text + "$]", conn);
DataTable dt = new DataTable();
mydata.Fill(dt);
dmrc.dataGridView2.DataSource = dt;
dmrc.Show();
int j = dmrc.dataGridView2.Rows.Count;
for (int i = 0; i < j ; i++)
{
string StrQuery = "INSERT INTO " + textBox1.Text + " VALUES ('" + dmrc.dataGridView2.Rows[i].Cells[0].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[1].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[2].Value + "' , '" + dmrc.dataGridView2.Rows[i].Cells[3].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[4].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[5].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[6].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[7].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[8].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[9].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[10].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[11].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[12].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[13].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[14].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[15].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[16].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[17].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[18].Value + "');";
try
{
string myConne = "datasource=localhost;database=dmrc;port=3306;username=root;password=root";
MySqlConnection conne = new MySqlConnection(myConne);
conne.Open();
using (MySqlCommand comm = new MySqlCommand(StrQuery, conne))
{
comm.ExecuteNonQuery();
}
conne.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
在这个按钮中,我的行数完全正常,但不是前一个
答案 0 :(得分:3)
要记住的一件事是,只有AllowUsersToAddRow属性设置为true时,“空”datagridview才有1条记录。否则,行计数将为0。
我认为您在检索Count
..
DataGridView1Name.Rows.Count