Làm cách nào tôi có thể xóa tất cả các hàng datagridview ngoại trừ các tiêu đề cột?Làm cách nào để xóa tất cả các hàng DataGridView khi tải biểu mẫu?
tôi đã cố gắng:
dataGridView1.Rows.clear();
nhưng nó không hoạt động.
tôi đã cố gắng để lặp qua các hàng và sử dụng phương pháp RemoveAt
, nhưng nó không loại bỏ tất cả các hàng:
private void Form5_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = true;
SqlConnection con = new SqlConnection(@"Data Source=.\myserver;Initial Catalog=test;Integrated Security=True");
adapter = new SqlDataAdapter("SELECT id as [#], description as [Description], unit as [Unit], amount as [Amount], unitPrice as [Unit Price], total as [Total] FROM tbl_poMaterials", con);
adapter.SelectCommand.CommandType = CommandType.Text;
DataTable tb = new DataTable();
adapter.Fill(tb);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
dataGridView1.DataSource = tb;
dataGridView1.Columns[0].Width = 30;
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[1].Width = 660;
for (int i = 0; i < tb.Rows.Count; i++)
{
tb.Rows.RemoveAt(i);
}
}
Xác định "nhưng nó không hoạt động", điều gì sẽ xảy ra? –
Có thể trùng lặp của [DataGridView.Clear()] (http://stackoverflow.com/questions/3744882/datagridview-clear) –