110,534
社区成员
发帖
与我相关
我的任务
分享
DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
Bind();
if (dt.Rows.Count == 0 && dataGridView1.DataSource == bindingSource1)
{
dataGridView1.DataSource = null;
for (int i = 0; i < 10; i++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);
row.Cells[0].Value = "";
row.Cells[1].Value = "";
row.Cells[2].Value = "";
row.Cells[3].Value = "";
dataGridView1.Rows.Add(row);
}
}
else if (dt.Rows.Count <= 10)
{
dataGridView1.ScrollBars = ScrollBars.None;
}
else
{
dataGridView1.ScrollBars = ScrollBars.Both;
}
}
public void Bind()
{
SqlConnection conn = new SqlConnection(@"Data Source=SYC\SQLEXPRESS;Initial Catalog=db_28;Integrated Security=True");
conn.Open();
SqlDataAdapter dap = new SqlDataAdapter("select * from tb_01", conn);
dt = new DataTable();
dap.Fill(dt);
conn.Close();
bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;
}
DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
Bind();
if (dt.Rows.Count == 0 && dataGridView1.DataSource == bindingSource1)
{
dataGridView1.DataSource = null;
for (int i = 0; i < 10; i++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);
row.Cells[0].Value = "";
row.Cells[1].Value = "";
row.Cells[2].Value = "";
row.Cells[3].Value = "";
dataGridView1.Rows.Add(row);
}
}
else if (dt.Rows.Count <= 10)
{
dataGridView1.ScrollBars = ScrollBars.None;
}
else
{
dataGridView1.ScrollBars = ScrollBars.Both;
}
}
public void Bind()
{
SqlConnection conn = new SqlConnection(@"Data Source=SYC\SQLEXPRESS;Initial Catalog=db_28;Integrated Security=True");
conn.Open();
SqlDataAdapter dap = new SqlDataAdapter("select * from tb_01", conn);
dt = new DataTable();
dap.Fill(dt);
conn.Close();
bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;
}
public Materiel()
{
InitializeComponent();
//dataGridView1.ColumnCount = 6;
//dataGridView1.RowCount = 6;
mySelectCommand = myConnection.CreateCommand();
myInsertCommand = myConnection.CreateCommand();
myDeleteCommand = myConnection.CreateCommand();
myUpdateCommand = myConnection.CreateCommand();
mySelectCommand.CommandText = "SELECT materiel_Id, materielType, materielPrice,materielQuantity,materielStuff,materielLength,materielWidth,materielHeight,materielColor " +
"FROM materiel " +
"ORDER BY materiel_Id";
mySqlDataAdapter.SelectCommand = mySelectCommand;
mySqlDataAdapter.Fill(myDataSet, "materiel");
dataGridView1.DataSource = myDataSet.Tables["materiel"];
if (dataGridView1.RowCount < 10)
{
DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex];
dataGridView1.Rows.Add(dr);
}
}