请再帮我看看这段代码:
public class TStringGrid:System.Web .UI .WebControls .DataGrid   
{
private int FixRow=8;
private int FixCol=5;
private DataTable Table;
public DataRowCollection Rows ;
public DataColumnCollection Columns ; public TStringGrid():base()
{
} public void Create()
{
for(int i=0;i<FixCol;i++)
{
   System.Data .DataColumn column=new DataColumn ();
   this.Columns.Add(column);
}
for(int j=0;j<FixRow;j++)
{
DataRow newrow=Table.NewRow ();
this.Rows.Add(newrow);
}
}
有没有问题?
但到引用时,就有问题:Label1.Text =sg.Columns .Count .ToString ();
问题提示:System.NullReferenceException: 未将对象引用设置到对象的实例
请问做如何修改啦?

解决方案 »

  1.   

    sg是什么?声明了么?
    Label1没有声明就用也会出错
    还有,最重要,确保sg已经绑定了数据(即使数据是空值也要绑)!
      

  2.   

    往DataTable中加数据,不知道是不是你要的
    DataTable tb=new DataTable();
    DataRow dr;
    tb.Columns.Add(new DataColumn("filename",typeof(string)));
    tb.Columns.Add(new DataColumn("downid",typeof(Int32)));
    tb.Columns.Add(new DataColumn("filetype",typeof(string)));
    tb.Columns.Add(new DataColumn("upload",typeof(string)));
    tb.Columns.Add(new DataColumn("IsExistDwfDwg",typeof(bool)));
    dr = tb.NewRow();
    dr[0]=strFileName;
    dr[1]=intdownid;
    dr[2]="文件";
    dr[3]="["+base.Visitor.UserName+"]"+base.Visitor.UserAccount;
    dr[4]=false;
    tb.Rows.Add(dr);