Jak usunąć wszystkie wiersze datagridview oprócz nagłówków kolumn?Jak usunąć wszystkie wiersze DataGridView podczas ładowania formularza?
Próbowałem:
dataGridView1.Rows.clear();
ale to nie działa.
Próbowałem pętli nad rzędami i użyć metody RemoveAt
, ale nie usuwa wszystkie wiersze:
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);
}
}
Define "ale to nie działa", co się dzieje? –
Możliwy duplikat [DataGridView.Clear()] (http://stackoverflow.com/questions/3744882/datagridview-clear) –