c#中数据视图的行数
本文关键字:视图 数据 | 更新日期: 2023-09-27 18:12:54
我有一个表在我的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);
}
在这个按钮中显示行。COUNT工作正常,但不是在前一个
要记住的一件事是,只有当AllowUsersToAddRow属性设置为true时,"空"datagridview才有1条记录。否则,行数将为0。
来源我想你也对检索Count
的语法感到困惑。
DataGridView1Name.Rows.Count