C# dataGridView绑定DataTable数据时,如果在清空总行数时保留列头?

string sql = "select UrlId as '编号',Urllianjie as '名称',Urlbeizhu as '性别' from T_UrL";
SqlConnection connStr = YTCommandinUdy.YTCnonntionY .PCnonntion ("LoginTest");
DataTable dtable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql, connStr);
da.Fill(dtable);
this.dataGridView1.DataSource = dtable.DefaultView; 如果 我 dataGridView1.Rows.Clear();则会报错,不能清除列头,有什么方法,即可清空又可保留下列头

int k = this.dataGridView1.Rows.Count-1;//最后一行默认空行
if (dataGridView1.Rows.Count > 0)
{
for (int i = k; i >= 1; i--)//从下往上删,避免沙漏效应
{
this.dataGridView1.Rows.RemoveAt(dataGridView1.Rows[i - 1].Index);
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜