【问题标题】:DataTable does not get assigned未分配 DataTable
【发布时间】:2013-09-28 06:59:14
【问题描述】:

button_click 事件被触发时。我从方法中得到DataTable 并将其分配给类中定义的DataTable,但是这个DataTable 没有设置。如果我将DataTable 绑定到GridView 它会显示数据。

public partial class Default : System.Web.UI.Page
{
  TestClass test = new TestClass();
  DataTable _table = new DataTable();

  protected void Button1_Click(object sender, EventArgs e)
    {

      _table = test.getTable();
     //displaying table
      GridView1.DataSource= _table;
      GridView1.DataBind();
    }
  protected void Button2_Click(object sender, EventArgs e)
    {
      //does not displaying table 
      GridView2.DataSource= _table;
      GridView2.DataBind();
    }
}

当我想从_table 获取一些数据时,它什么都没有。怎么了?

更新: 谢谢大家,尤其是Darren Davies,问题出在回传中。当 Button2_Click 事件发生时 _table 分配 _table = new DataTable(),因此 _table 不再引用 getTable() 方法返回的表。

【问题讨论】:

  • 你确定 getTable() 返回数据吗?
  • 我调试了它,_table 有行 [count = 54],但是我点击 button2 行有 count = 0。
  • @AlbertGore - 很可能发生了回发。您需要再次致电getTable

标签: c# .net sql-server datatable


【解决方案1】:

只有在 Button2 之前点击 Button1 才会正确绑定。否则_table 将不会被填充。

您应该检查_table 是否有数据或在Button2_Click 事件处理程序中调用getTable 方法:

protected void Button2_Click(object sender, EventArgs e)
{
   _table = test.getTable();
   GridView2.DataSource= _table;
   GridView2.DataBind();
}

【讨论】:

    【解决方案2】:

    你可以试试这个:

        protected void Button1_Click(object sender, EventArgs e)
            {
    
              _table = test.getTable();
             //displaying table
             GridVieuw1.Datasource = null;
             GridView1.DataSource= _table;
             GridView1.DataBind();
    
            }
    

    您还必须在按钮 2 处执行此操作

    protected void Button2_Click(object sender, EventArgs e)
                {
    
                  _table = test.getTable();
                 //displaying table
                 GridVieuw2.Datasource = null;
                 GridView2.DataSource= _table;
                 GridView2.DataBind();
    
                }
    

    否则表格在按钮 2 处是空的,所以你必须按下按钮 1 来填充它

    也许这可以解决问题

    【讨论】:

      【解决方案3】:

      你正在初始化你的 DataTable 喜欢

      DataTable _table = new DataTable();
      

      然后,当您单击按钮 1 时:

      protected void Button1_Click(object sender, EventArgs e)
      {
        _table = test.getTable(); // <- here
       //displaying table
        GridView1.DataSource= _table;
        GridView1.DataBind();
      }
      

      您正在将您的DataTable 分配给一个不同的 实例。 您必须调试并检查您的方法 getTable 是否返回有效的 DataTable(如果您想查看数据,请确保它包含数据。

      【讨论】:

        【解决方案4】:

        当您没有在按钮 2 之前按下按钮 1 时,就会出现问题。您应该像这样概括您的数据绑定:

        public partial class Default : System.Web.UI.Page
        {
          TestClass test = new TestClass();
          DataTable _table = new DataTable();
        
          protected void Button1_Click(object sender, EventArgs e)
            {
        
              FillDataTable ();    // <----------- See this function call
        
             //displaying table
              GridView1.DataSource= _table;
              GridView1.DataBind();
            }
          protected void Button2_Click(object sender, EventArgs e)
            {
        
              FillDataTable ();    // <----------- See this function call
        
              //does not displaying table 
              GridView2.DataSource= _table;
              GridView2.DataBind();
            }
        
            private void FillDataTable()
            {
                 if(_table.Rows.Count == 0) 
                   _table = test.getTable();
            }
        }
        

        明智的做法是填写一次表格,而不是在每次按钮点击时填写并在每次按钮点击事件中记录下来。

        【讨论】:

          猜你喜欢
          • 2011-10-02
          • 1970-01-01
          • 2018-02-20
          • 2012-02-09
          • 1970-01-01
          • 2011-09-15
          • 2012-10-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多