ICollection  AddRow()
{
DataRow dr = MyDt.NewRow(); dr[0] = TbLine.Text ;
dr[1] = DropDownList1.SelectedItem.Text ;
dr[2] = DropDownList2.SelectedItem.Text ;
dr[3] = TbNumber.Text ;
dr[4] = TbSinglePrice.Text ;
dr[5] = TbTotalPrice.Text ;
dr[6] = Tbfactory.Text ;
dr[7] = TbSupply.Text ;
dr[8] = TbSupplyOutId.Text ;
dr[9] = TbSupplyOutFp.Text ;
dr[10] = TbTime.Text ;

MyDt.Rows.Add(dr); DataView dv = new DataView(MyDt); DropDownList1.SelectedIndex=0;
DropDownList2.SelectedIndex=0;
TbNumber.Text = "" ;
TbSinglePrice.Text = "" ;
TbTotalPrice.Text = "" ;
Tbfactory.Text = "" ;
TbSupply.Text = "" ;
TbSupplyOutId.Text = "" ;
TbSupplyOutFp.Text = "" ;
TbTime.Text = "" ; return dv ;
} DataTable CreateColumns() 
{
//DataTable MyDt = new DataTable();
MyDt = new DataTable(); DataColumn col=new DataColumn("入库流水号", typeof(string));
col.AllowDBNull=false;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("耗材类型名称", typeof(string));
col.AllowDBNull=false;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("耗材型号名称", typeof(string));
col.AllowDBNull=false;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("数量", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("单价", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("总价", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col);

col=new DataColumn("生产厂家", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("供应商", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("供应商出库号", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col);

col=new DataColumn("供应商发票号", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("购置日期", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); //AddRow();
MyDt.AcceptChanges();
//DataRow dr = MyDt.NewRow(); return MyDt;
} private void Page_Load(object sender, System.EventArgs e)
{
myInfo =new GetMySysteminfo ();
myInfo.myPage = this ;
if (!myInfo.ValidateInfo()) 
return ; if (!IsPostBack) 
{
InitDropDownListExpendType();     Calendar1.SelectionMode=CalendarSelectionMode.Day;     Calendar1.ShowNextPrevMonth=true;

MyDataGrid.Width=1500 ;
MyDataGrid.DataSource= CreateColumns();
MyDataGrid.DataBind();
}

InitLine();
}

解决方案 »

  1.   

    DataTable MyDt已经声明是我的类成员了
      

  2.   

    ICollection  AddRow()
    {
    DataRow dr = MyDt.NewRow();是在这里报错的怎么解决各位老大!
      

  3.   

    IDataRow dr =new datarow();
      

  4.   

    DataTable CreateColumns() 
    {
    //DataTable MyDt = new DataTable();
    MyDt = new DataTable();这里创建的啊
      

  5.   

    DataRow dr = MyDt.NewRow();
    在这句设个断点看一下,MyDt可能由于什么原因变成null了.
      

  6.   

    确何CreateColumns()在AddRow()前要运行过.
    代码测试正常,测试代码如下:
    namespace TEST
    {
      using System.Data;
      using System.Collections;  class T
      {
      public T()
      {
      }    ICollection  AddRow()
        {
          DataRow dr = MyDt.NewRow();
          dr[0 ] = "111";
          dr[1 ] = "";
          dr[2 ] = "";
          dr[3 ] = "";
          dr[4 ] = "";
          dr[5 ] = "";
          dr[6 ] = "";
          dr[7 ] = "";
          dr[8 ] = "";
          dr[9 ] = "";
          dr[10] = "";      MyDt.Rows.Add(dr);
          DataView dv = new DataView(MyDt);
          System.Windows.Forms.MessageBox.Show(dv[0]["入库流水号"].ToString());
          return dv ;
        }    DataTable  MyDt;
        DataTable CreateColumns()
        {
          MyDt = new DataTable();      DataColumn col=new DataColumn("入库流水号", typeof(string));
          col.AllowDBNull=false;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("耗材类型名称", typeof(string));
          col.AllowDBNull=false;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("耗材型号名称", typeof(string));
          col.AllowDBNull=false;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("数量", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("单价", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("总价", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("生产厂家", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("供应商", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("供应商出库号", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("供应商发票号", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      col=new DataColumn("购置日期", typeof(string));
          col.AllowDBNull=true;
          col.MaxLength=100;
          MyDt.Columns.Add(col);      MyDt.AcceptChanges();      return MyDt;
        }
        static void Main()
        {
         T t = new T();
         t.CreateColumns();
         t.AddRow();
        }
      }
    }
      

  7.   

    构造数据表有个好办法,可以用下面的查询语句实现,不要那么多的语句的,“一般人我不告诉他!”,呵呵~~~~~~~~
    select 
    '' as 入库流水号,
    '' as 耗材类型名称,
    '' as 耗材型号名称,
    '' as 数量,
    '' as 单价,
    '' as 总价,
    '' as 生产厂家,
    '' as 供应商,
    '' as 供应商出库号,
    '' as 供应商发票号,
    '' as 购置日期
    where 1=2