把不能编辑的列ReadOnly设为True

解决方案 »

  1.   

    那怎么隐藏掉DataGrid的自动增长行功能呢?路过的感兴趣的顶一下。
      

  2.   

    只读列:
    DataGridColumnStyle.ReadOnly = true;取消自动增行:
    通过将 DataView 用作数据源并将 AddNew 属性设置为 false,可以创建网格,该网格允许用户编辑数据但阻止他们添加新行。
      

  3.   

    把不能编辑的列ReadOnly设为True
    同时把绑定到datagrid的dataview的AllowNew属性设为false
      

  4.   

    我现在用DataAdapter生成一Table。并通过DataView邦定到DataGrid上,可以不让其增加新行,但是自己如何向DataGrid添加与 生成table 关联的GridTableStyles ????
      

  5.   

    private string[] arrstr ;
    private DataTable dt=new DataTable();
    private DataRow myDataRow;
    private DataSet ds=new DataSet();
    private DataColumn myDataColumn;private void SetUp_DataGrid()
    {
    arrstr = new string [11];
    arrstr[0] = "DrugName";//药品名称
    arrstr[1] = "DrugSpec";//药品规格
    arrstr[2] = "Unit";//单位
    arrstr[3] = "DrugNo";//批次号
    arrstr[4] = "ProductArea";//产地
    arrstr[5] = "StoreNum";//库存数量
    arrstr[6] = "NoAccountNum";//未登帐数
    arrstr[7] = "FactNum";//出库数量
    arrstr[8] = "SalePrice";//零售单价
    arrstr[9] = "SaleMoney";//零售金额
    arrstr[10] = "DrugPriceID";//药品价格ID号 dt.TableName="myTable";
    dt.DefaultView.AllowNew=false;//去掉多余的空行 dg.DataSource=dt;
    dg.AllowSorting=false; for(int i=0;i<arrstr.Length;i++)
    {
    myDataColumn=new DataColumn();
    myDataColumn.DataType=System.Type.GetType("System.String");
    myDataColumn.ColumnName=arrstr[i];
    dt.Columns.Add(myDataColumn);
    }
               
    DataGridTableStyle style=new DataGridTableStyle();
    style.MappingName="myTable"; style.AllowSorting=false; style.HeaderBackColor=Color.DarkBlue;
    style.HeaderForeColor=Color.White; style.RowHeadersVisible=false; style.BackColor=Color.LightYellow;
    style.AlternatingBackColor=Color.PaleGoldenrod; style.GridLineColor=Color.Black;
    style.GridLineStyle=DataGridLineStyle.Solid; MyDataGridSearchEditColumn col1=new MyDataGridSearchEditColumn(dg);
    col1.MappingName="DrugName";
    col1.HeaderText="药品名称";
    col1.Width=120;
    col1.Column_BackColor=Color.LightGreen;
    col1.Column_ForeColor=Color.Black;
    col1.Set_Color();
                                col1.Show_DrugName(frmInputType.GetInputType(),frmLogin.s_objDrugLists);
    col1.EnterKeyPress +=new _ISearchEditEvents_KeyPressEventHandler(this.EditColumnPress);
    MyDataGridTextColumn col2=new MyDataGridTextColumn(dg);
    col2.MappingName="DrugSpec";
    col2.HeaderText="药品规格";
    col2.Width=100;
    col2.m_textbox.ReadOnly=true;
    col2.Column_BackColor=Color.LightGreen;
    col2.Column_ForeColor=Color.Black;
    col2.EnterKeyPress +=new EventHandler(this.CellEnterPress); MyDataGridTextColumn col3=new MyDataGridTextColumn(dg);
    col3.MappingName="Unit";
    col3.HeaderText="单位";
    col3.Width=35;
    col3.m_textbox.ReadOnly=true;
    col3.Column_BackColor=Color.LightGreen;
    col3.Column_ForeColor=Color.Black;
    col3.EnterKeyPress +=new EventHandler(this.CellEnterPress); MyDataGridTextColumn col4=new MyDataGridTextColumn(dg);
    col4.MappingName="DrugNo";
    col4.HeaderText="批次号";
    col4.Width=80;
    col4.m_textbox.ReadOnly=true;
    col4.Column_BackColor=Color.LightGreen;
    col4.Column_ForeColor=Color.Black;
    col4.EnterKeyPress +=new EventHandler(this.CellEnterPress); MyDataGridTextColumn col5=new MyDataGridTextColumn(dg);
    col5.MappingName="ProductArea";
    col5.HeaderText="产地";
    col5.Width=80;
    col5.m_textbox.ReadOnly=true;
    col5.Column_BackColor=Color.LightGreen;
    col5.Column_ForeColor=Color.Black;
    col5.EnterKeyPress +=new EventHandler(this.CellEnterPress); MyDataGridTextColumn col6=new MyDataGridTextColumn(dg);
    col6.MappingName="StoreNum";
    col6.HeaderText="库存数量";
    col6.Width=80;
    col6.m_textbox.ReadOnly=true;
    col6.Column_BackColor=Color.LightGreen;
    col6.Column_ForeColor=Color.Black;
    col6.EnterKeyPress +=new EventHandler(CellEnterPress); MyDataGridTextColumn col7=new MyDataGridTextColumn(dg);
    col7.MappingName="NoAccountNum";
    col7.HeaderText="未登帐数";
    col7.Width=80;
    col7.m_textbox.ReadOnly=true;
    col7.Column_BackColor=Color.LightGreen;
    col7.Column_ForeColor=Color.Black;
    col7.EnterKeyPress +=new EventHandler(this.CellEnterPress); MyDataGridTextColumn col8=new MyDataGridTextColumn(dg);
    col8.MappingName="FactNum";
    col8.HeaderText="出库数量";
    col8.Width=80;
    col8.Column_BackColor=Color.LightGreen;
    col8.Column_ForeColor=Color.Black;
    col8.EnterKeyPress +=new EventHandler(this.CellEnterPress);
    col8.m_textbox.Leave += new EventHandler(this.TextBox_Leave); MyDataGridTextColumn col9=new MyDataGridTextColumn(dg);
    col9.MappingName="SalePrice";
    col9.HeaderText="零售单价";
    col9.Width=80;
    col9.m_textbox.ReadOnly=true;
    col9.Column_BackColor=Color.LightGreen;
    col9.Column_ForeColor=Color.Black;
    col9.EnterKeyPress +=new EventHandler(this.CellEnterPress); MyDataGridTextColumn col10=new MyDataGridTextColumn(dg);
    col10.MappingName="SaleMoney";
    col10.HeaderText="零售金额";
    col10.Width=100;
    col10.m_textbox.ReadOnly=true;
    col10.Column_BackColor=Color.LightGreen;
    col10.Column_ForeColor=Color.Black;
    col10.EnterKeyPress +=new EventHandler(this.CellEnterPress);
    style.GridColumnStyles.Add(col1);
    style.GridColumnStyles.Add(col2);
    style.GridColumnStyles.Add(col3);
    style.GridColumnStyles.Add(col4);
    style.GridColumnStyles.Add(col5);
    style.GridColumnStyles.Add(col6);
    style.GridColumnStyles.Add(col7);
    style.GridColumnStyles.Add(col8);
    style.GridColumnStyles.Add(col9);
    style.GridColumnStyles.Add(col10);

    dg.TableStyles.Add(style);
    }
      

  6.   

    那些MyDataGridTextColumn,MyDataGridSearchEditColumn是我自己继承DataGridTextBoxColumn重新写的类
      

  7.   

    把不能编辑的列ReadOnly设为True
    同时把绑定到datagrid的dataview的AllowNew属性设为falsethis.dataGridTableStyle1.DataGrid = this.dataGrid1;
    this.dataGridTableStyle1.MappingName = 你的DataTable的名字;
      

  8.   

    就是楼上说的,实现过程很清楚
    DataView dv = new ("table1");
    dv..AllowNew = false;
    如果要一些行不能修改,
    this.dataGrid1.TableStyles[0].GridColumnStyles[行号].ReadOnly = true;
      

  9.   

    this.dataGrid1.TableStyles[0].GridColumnStyles[列号].ReadOnly = true;
    这里的TableStyles[0]表示你只有一个DataTableStyle。
    如果你的DataGrid1中有多个DataTableStyle那就要通过索引访问了。
    TableStyles[i]
    或者
    TableStyles[“tableName”]