也就是说如果我没有数据时就清除数据,有就显示,

解决方案 »

  1.   

    dataGridView.Rows.Clear();
    //or
    dataGridView.DataSource = null;
      

  2.   

    补充说明一下:
    我的dataGridView是手动写上的中文列名,不能去在SQL中AS为中文名。
    用的数据绑定。dataGridView.Rows.Clear();      //如果是这样,提示“不能清除此列表”
    //or
    dataGridView.DataSource = null;//再次显示时,我加入的字段全是英文了
      

  3.   

    这样不会清空格式:DataTable dt = (DataTable)dataGridView.DataSource;
    if (dt!=null)
    {
         dt.Rows.Clear();
    }
      

  4.   

    DataTable dt = (DataTable)dataGridView.DataSource;//转换不了
    if (dt!=null)
    {
         dt.Rows.Clear();
    }
    以下是我的代码:
    DataSet tempDataSet = new DataSet ( );
    BindingSource bind = new BindingSource ( );tempDataSet = "sql"//省略了
    if ( tempDataSet.Tables [ 0 ].Rows.Count > 0 )
    {
      bind.Clear ( );
      bind.DataSource = tempDataSet.Tables [ 0 ];  hDatagridview2.DataBindings.Clear ( );
      this.hDatagridview2.DataSource = bind;
    }
    else//清不了
    {
      hDatagridview2.DataBindings.Clear ( );
      hDatagridview2.Update ( );
      hDatagridview2.Refresh ( );
     }
      

  5.   

    重置列标题,参考如下:
    dataGridView1.DataSource = null;
    dataGridView1.Columns[i].HeaderText="中文";
    ......
      

  6.   

    清除 DataTable
    然后 
    SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
    {
        DataRowView rowView = view.AddNew();
        // Change values in the DataRow.
        rowView["列名1"] = reader[0].ToString().Trim();
        rowView["列名2"] = reader[1].ToString().Trim();
        rowView.EndEdit();
    }
      

  7.   

    etherealkite 如果是你那样弄还有必要问了吗?兄弟啊!!
    lf44785170 兄弟啊,我如果有20列就要写20多次吗??
    有没有再好点的方法了!
      

  8.   

    dataGridView.DataSource = null;这种方法可行
    你处理sql查询语句就可以了,select 英文列名 as 中文列名,... from 表名,这样做你再次设置数据源的时候就不会变成英文了。
      

  9.   

    你把你的数据源给删了!
    dgvname.DataSource = list;
    list.Sort();
    dgvname.DataSource=new List<Student>();
    dgvname.DataSource = list;想当于这个原理!!!你实例一个空的表把原来的复盖掉再填冲!!!