C# dataGridView绑定DataTable数据时,如果在清空总行数时保留列头?
stringsql="selectUrlIdas'编号',Urllianjieas'名称',Urlbeizhuas'性别'fromT_UrL";SqlConnection...
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();则会报错,不能清除列头,有什么方法,即可清空又可保留下列头 展开
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();则会报错,不能清除列头,有什么方法,即可清空又可保留下列头 展开
2个回答
2013-04-11
展开全部
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);
}
}
if (dataGridView1.Rows.Count > 0)
{
for (int i = k; i >= 1; i--)//从下往上删,避免沙漏效应
{
this.dataGridView1.Rows.RemoveAt(dataGridView1.Rows[i - 1].Index);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询