把不能编辑的列ReadOnly设为True
解决方案 »
- 关于GoogleTranslateAPI,高手来吧
- GirdView 怎么自动生成模板列?生成的模板列后要怎么得到这些模板列的控件值??
- 微软的Enterprise Application Block 在 vs2005下写的程序,引用运行总是出错,同样在vs 2003下运行良好,大家碰到过么?
- 返回值问题
- 怎么把XML中的一个结点值加到EXCEL中的一行里
- 关于单系统,多站点的问题
- json序列的解析。。
- 请大侠指教!!~
- 高分求解:属性、索引器或事件“Names”不受该语言支持;请尝试直接调用访问器方法 。。。。
- StringFormat 为什么没起作用
- 我想让服务器的exe程序每隔一段时间去扫描一次数据表,但不要影响服务器性能?
- 怎样知道数据库中数据变了?
DataGridColumnStyle.ReadOnly = true;取消自动增行:
通过将 DataView 用作数据源并将 AddNew 属性设置为 false,可以创建网格,该网格允许用户编辑数据但阻止他们添加新行。
同时把绑定到datagrid的dataview的AllowNew属性设为false
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);
}
同时把绑定到datagrid的dataview的AllowNew属性设为falsethis.dataGridTableStyle1.DataGrid = this.dataGrid1;
this.dataGridTableStyle1.MappingName = 你的DataTable的名字;
DataView dv = new ("table1");
dv..AllowNew = false;
如果要一些行不能修改,
this.dataGrid1.TableStyles[0].GridColumnStyles[行号].ReadOnly = true;
这里的TableStyles[0]表示你只有一个DataTableStyle。
如果你的DataGrid1中有多个DataTableStyle那就要通过索引访问了。
TableStyles[i]
或者
TableStyles[“tableName”]