在c#里怎么隐藏datagrid里不要的列啊
用代码怎么设置每列的宽度呢?
谢谢大家!!!

解决方案 »

  1.   

    在select语句的时候把不要的列不要写景去不就可以了
      

  2.   

    datagrid.Columns.Item(0).Visible = False 第一列隐藏
      

  3.   

    设置列宽度为0;if(show)
        this.dataGridTextBoxColumnName.Width = 75;
    else
        this.dataGridTextBoxColumnName.Width = 0;
      

  4.   

    在设计期属性页里,把列的可见性的勾去掉
    -------------------------
    代码控制:
    在datagrid的ItemDataBound事件里写
    比如:
    private void dgd_temp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    LinkButton btn_del;
             if(e.Item.ItemType ==  ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    {
    btn_del = (LinkButton)e.Item.FindControl("btn_del");
    btn_del.Attributes.Add("onclick","return confirm('你真的要删除吗?');");
    }
    }-----
    e.Item.Cells[i]---访问单元格
    -----
    DataGrid1.Columns[i].ItemStyle.Width--列宽
      

  5.   

    窗体设计器提供的代码里面有
    this.objTableStylegrdIssuesIssues.GridColumnStyles.AddRange这个方法,去掉里面不需要的列
    宽度也在自动生成的代码中可以找到,Width属性,不难发现
      

  6.   

    DataGrid1.TableStyles["Products"].GridColumnStyles["QuantityPerUnit"].Width = 0
    或者
    ds.Tables["Products"].Columns["QuantityPerUnit"].ColumnMapping = MappingType.Hidden;
    DataGrid1.SetDataBinding[ds, "Products"];
      

  7.   

    [推荐]WebForm中DataGrid的20篇经典文章 
    http://community.csdn.net/Expert/topic/3896/3896149.xml?temp=.5802423
      

  8.   

    datagrid.Colunms[i].Visible = false;
      

  9.   

    问题不明确,是WINFORM OR WEBFORM
      

  10.   

    *********************************************************************
    3.cs中,设置DataGrid控件的莫列的宽度为0,让其不显示出来DataGridTableStyle MyStyle =new DataGridTableStyle();   //定义表样式对象
    DataTable tableName = (DataTable)this.dgDoc.DataSource;  //获取表对象
    MyStyle.MappingName = tableName.TableName;               //把表的名字传给样式对象的属性(必要的)
    dgDoc.TableStyles.Add(MyStyle);                         //把样式添加到DataGrid控件中
    MyStyle.GridColumnStyles["编号"].Width=0;               //设置莫行不显示的列的宽度为0 
    MyStyle.GridColumnStyles["文档名称"].Width=400;
      

  11.   

    datagrid.Columns.Item[0].Visible = False
      

  12.   


    codeproject.com 有根据记录内容自动调整列宽的!效果不错public  void SizeColumnsToContent (DataGrid dataGrid, int nRowsToScan) 
                                       //dataGrid 就是要绑定的dataGrid 的Name;nRowsToScan=-1 表示对所有的记录都自动调整列宽
    {
    // Create graphics object for measuring widths.
    Graphics Graphics = dataGrid.CreateGraphics(); // Define new table style.
    DataGridTableStyle tableStyle = new DataGridTableStyle(); try
    {
    DataTable dataTable = (DataTable)dataGrid.DataSource; if (-1 == nRowsToScan)
    {
    nRowsToScan = dataTable.Rows.Count;
    }
    else
    {
    // Can only scan rows if they exist.
    nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
    } // Clear any existing table styles.
    dataGrid.TableStyles.Clear(); // Use mapping name that is defined in the data source.
    tableStyle.MappingName = dataTable.TableName; // Now create the column styles within the table style.
    DataGridTextBoxColumn columnStyle;
    int iWidth; for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)
    {
    DataColumn dataColumn = dataTable.Columns[iCurrCol]; columnStyle = new DataGridTextBoxColumn(); columnStyle.TextBox.Enabled = true;
    columnStyle.HeaderText = dataColumn.ColumnName;
    columnStyle.MappingName = dataColumn.ColumnName; // Set width to header text width.
    iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width); // Change width, if data width is wider than header text width.
    // Check the width of the data in the first X rows.
    DataRow dataRow;
    for (int iRow = 0; iRow < nRowsToScan; iRow++)
    {
    dataRow = dataTable.Rows[iRow]; if (null != dataRow[dataColumn.ColumnName])
    {
    int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
    iWidth = (int)System.Math.Max(iWidth, iColWidth);
    }
    }
    columnStyle.Width = iWidth + 4; // Add the new column style to the table style.
    tableStyle.GridColumnStyles.Add(columnStyle);
    }
    // Add the new table style to the data grid.
    dataGrid.TableStyles.Add(tableStyle);
    }
    catch(Exception e)
    {
    MessageBox.Show(e.Message);
    }
    finally
    {
    Graphics.Dispose();
    }
    }
      

  13.   

    上面几为大侠的方法太过复杂,只要编辑DATAGRID的列的属性即可,把Visible那个钩去掉就可以了!
      

  14.   

    在datagrid里的属性设置,也可以在取数据的时候控制,最好不要在ItemDataBound事件里写东西,后期绑带费资源
      

  15.   


    用不着那么复杂的方法吧Win Form用列集合的属性绑定需要的字段Web 里用属性生成器绑定需要的字段就可以