Gridview Data not binded on second time execution.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hydson
    New Member
    • Feb 2008
    • 4

    Gridview Data not binded on second time execution.

    Hi,

    I have two gridviews to which data is binded when page is not posted back.

    when i run for first time the data is binded to gridview, and when i run next time the data is not binded to grid view.

    the code is:


    public partial class _Default : System.Web.UI.P age

    {

    public static DataTable tableGrid1 = new DataTable();

    protected void Page_Load(objec t sender, EventArgs e)

    {

    if (!IsPostBack)

    {

    BindData();

    }

    }



    public void BindData()

    {



    DataColumn columnGrid1 = new DataColumn(); // Binding data to the tableGrid1

    columnGrid1.Col umnName = "TeamName";

    tableGrid1.Colu mns.Add(columnG rid1);



    DataRow dr1 = tableGrid1.NewR ow();

    dr1[0] = "AAAAAAA";

    tableGrid1.Rows .Add(dr1);

    DataRow dr2 = tableGrid1.NewR ow();

    dr2[0] = "BBBBBBBB";

    tableGrid1.Rows .Add(dr2);

    DataRow dr3 = tableGrid1.NewR ow();

    dr3[0] = "CCCCCCC";

    tableGrid1.Rows .Add(dr3);

    DataRow dr4 = tableGrid1.NewR ow();

    dr4[0] = "DDDDDDDDD" ;

    tableGrid1.Rows .Add(dr4);

    DataRow dr5 = tableGrid1.NewR ow();

    dr5[0] = "EEEEEEEEE" ;

    tableGrid1.Rows .Add(dr5);



    tableGrid1.Rows .Add(tableGrid1 .NewRow());



    GridView1.DataS ource = tableGrid1;

    GridView1.DataB ind();

    }

    }



    when i run second time without rebuild it is not working

    pls help me...

    i m going mad !!!

    bye
    hydson
  • int08h
    New Member
    • Apr 2007
    • 28

    #2
    set a breakpoint at line "if (!IsPostBack)" and observe whether IsPostBack is true or false, if it is true the second time you refresh the page, it is majorly a problem with your web browser

    Comment

    • dip_developer
      Recognized Expert Contributor
      • Aug 2006
      • 648

      #3
      Originally posted by hydson
      Hi,

      I have two gridviews to which data is binded when page is not posted back.

      when i run for first time the data is binded to gridview, and when i run next time the data is not binded to grid view.

      the code is:


      public partial class _Default : System.Web.UI.P age

      {

      public static DataTable tableGrid1 = new DataTable();

      protected void Page_Load(objec t sender, EventArgs e)

      {

      if (!IsPostBack)

      {

      BindData();

      }

      }



      public void BindData()

      {



      DataColumn columnGrid1 = new DataColumn(); // Binding data to the tableGrid1

      columnGrid1.Col umnName = "TeamName";

      tableGrid1.Colu mns.Add(columnG rid1);



      DataRow dr1 = tableGrid1.NewR ow();

      dr1[0] = "AAAAAAA";

      tableGrid1.Rows .Add(dr1);

      DataRow dr2 = tableGrid1.NewR ow();

      dr2[0] = "BBBBBBBB";

      tableGrid1.Rows .Add(dr2);

      DataRow dr3 = tableGrid1.NewR ow();

      dr3[0] = "CCCCCCC";

      tableGrid1.Rows .Add(dr3);

      DataRow dr4 = tableGrid1.NewR ow();

      dr4[0] = "DDDDDDDDD" ;

      tableGrid1.Rows .Add(dr4);

      DataRow dr5 = tableGrid1.NewR ow();

      dr5[0] = "EEEEEEEEE" ;

      tableGrid1.Rows .Add(dr5);



      tableGrid1.Rows .Add(tableGrid1 .NewRow());



      GridView1.DataS ource = tableGrid1;

      GridView1.DataB ind();

      }

      }



      when i run second time without rebuild it is not working

      pls help me...

      i m going mad !!!

      bye
      hydson
      check whether Gridview's EnableViewState property is True or False.......... ..if False ...make it to True

      Comment

      • hydson
        New Member
        • Feb 2008
        • 4

        #4
        Originally posted by int08h
        set a breakpoint at line "if (!IsPostBack)" and observe whether IsPostBack is true or false, if it is true the second time you refresh the page, it is majorly a problem with your web browser


        hi thanks

        i checked second time and its true..

        wat can be the problem wid the browser ???

        thanks

        hydson :-)

        Comment

        • hydson
          New Member
          • Feb 2008
          • 4

          #5
          Originally posted by dip_developer
          check whether Gridview's EnableViewState property is True or False.......... ..if False ...make it to True

          hi thanks a lot

          enableview state is true only

          pls advice further...

          bye

          hydson.

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            Use ur bind function out of !Ispostback
            or

            define ur datatable object tablegrid in bind function

            Comment

            • hydson
              New Member
              • Feb 2008
              • 4

              #7
              Originally posted by kunal pawar
              Use ur bind function out of !Ispostback
              or

              define ur datatable object tablegrid in bind function




              hi i want to give full description of wat i am doing.

              i have 2 grdiviews .

              one is assigned another unassigned

              i have some teams binded to grid1 . now on click n dbl click i need to bind them to grid2.

              on running all the teams are binded to grid1. and click n dbl click they are binded to grid2

              now if i close the window. and do start without debugging i m not finding any data in grid1.



              pls go thru dis code

              public partial class _Default : System.Web.UI.P age, IPostBackEventH andler

              {

              // Declaration of tables and static variables

              public static DataTable tableGrid1 = new DataTable();pub lic static DataTable tableGrid2 = new DataTable();


              DataColumn columnGrid2 = new DataColumn();

              static int j1 = 0;public static DataSet ds = new DataSet();


              protected void Page_Load(objec t sender, EventArgs e)
              {



              if (j1 == 0)
              {

              columnGrid2.Col umnName = "TeamName";
              tableGrid2.Colu mns.Add(columnG rid2);

              tableGrid2.Rows .Add(tableGrid2 .NewRow());

              GridView2.DataS ource = tableGrid2;

              GridView2.DataB ind();

              j1++;

              DataColumn columnGrid1 = new DataColumn(); // Binding data to the tableGrid1

              columnGrid1.Col umnName = "TeamName";
              tableGrid1.Colu mns.Add(columnG rid1);

              DataRow dr1 = tableGrid1.NewR ow();dr1[0] = "Sales";
              tableGrid1.Rows .Add(dr1);

              DataRow dr2 = tableGrid1.NewR ow();

              dr2[0] = "Deployment ";
              tableGrid1.Rows .Add(dr2);

              DataRow dr3 = tableGrid1.NewR ow();dr3[0] = "Maintenanc e";
              tableGrid1.Rows .Add(dr3);

              DataRow dr4 = tableGrid1.NewR ow();

              dr4[0] = "Marketing" ;
              tableGrid1.Rows .Add(dr4);

              DataRow dr5 = tableGrid1.NewR ow();dr5[0] = "Production ";
              tableGrid1.Rows .Add(dr5);

              tableGrid1.Rows .Add(tableGrid1 .NewRow());

              ds.Tables.Add(t ableGrid1);

              GridView1.DataS ource = ds;

              GridView1.DataB ind();

              }



              }

              protected DataTable deleteRowGrid1( string index, DataTable sourceTable) //function to delete a row in grid1

              {

              for (int i = 0; i < sourceTable.Row s.Count; i++)
              {

              if (i == int.Parse(index ))
              {

              sourceTable.Row s.RemoveAt(i);

              }



              }

              tableGrid1 = sourceTable;

              return sourceTable;
              }

              protected DataTable addNewRowGrid2( ) // function to add a row to grid2

              {

              DataRow newRow = tableGrid2.NewR ow();string hdnRowData = hdn2.Value.ToSt ring();
              newRow[0] = hdnRowData;

              tableGrid2.Rows .Add(newRow);

              return tableGrid2;
              }

              protected void Button1_Click(o bject sender, EventArgs e) // ButtonClick event which deletes selected row from grid1 and adds to Grid2

              {

              string hdnRowIndex = hdn1.Value.ToSt ring();


              foreach (GridViewRow row in GridView1.Rows)
              {

              if (row.RowIndex == int.Parse(hdnRo wIndex))
              {

              GridView1.DataS ource = deleteRowGrid1( hdnRowIndex, tableGrid1);

              GridView1.DataB ind();



              GridView2.DataS ource = addNewRowGrid2( ); // To add the selected row into assigned list

              GridView2.DataB ind();

              }

              }



              }

              protected void GridView1_RowDa taBound(object sender, GridViewRowEven tArgs e)
              {

              if (e.Row.RowType == DataControlRowT ype.DataRow)
              {

              e.Row.Attribute s.Add("onclick" , "SelectRowGrid1 ('" + e.Row.Cells[0].FindControl("l bl").ClientID + "'," + e.Row.DataItemI ndex.ToString() + ")");e.Row.Attr ibutes.Add("ond blclick", Page.ClientScri pt.GetPostBackE ventReference(t his, "1"));
              }

              }



              public void RaisePostBackEv ent(string eventArgument)
              {

              if (!string.IsNull OrEmpty(eventAr gument))
              {

              if (eventArgument == "1")
              {

              selectGrid1();

              }

              if (eventArgument == "2")
              {

              selectGrid2();

              }

              }

              }

              public void selectGrid1() // function to select a row on double click and delete from Grid1 and add to Grid2

              {

              string hdnRowIndex = hdn1.Value.ToSt ring();foreach (GridViewRow row in GridView1.Rows)
              {

              if (row.RowIndex == int.Parse(hdnRo wIndex))
              {

              GridView1.DataS ource = deleteRowGrid1( hdnRowIndex, tableGrid1);

              GridView1.DataB ind();



              GridView2.DataS ource = addNewRowGrid2( ); // To add the selected row to Grid2

              GridView2.DataB ind();

              }

              }



              }

              protected void GridView2_RowDa taBound(object sender, GridViewRowEven tArgs e)
              {

              if (e.Row.RowType == DataControlRowT ype.DataRow)
              {

              e.Row.Attribute s.Add("onclick" , "SelectRowGrid2 ('" + e.Row.Cells[0].FindControl("l bl2").ClientID + "'," + e.Row.DataItemI ndex.ToString() + ")");e.Row.Attr ibutes.Add("ond blclick", Page.ClientScri pt.GetPostBackE ventReference(t his, "2"));
              }

              }



              public void selectGrid2() // function to select a row on double click and delete from Grid2 and add to Grid1

              {

              string hdnRowIndex = hdn4.Value.ToSt ring();tableGri d2.Rows.RemoveA t(int.Parse(hdn RowIndex));


              GridView2.DataS ource = tableGrid2;

              GridView2.DataB ind();

              GridView1.DataS ource = addNewRowGrid1( );

              GridView1.DataB ind();

              }

              protected void Button3_Click(o bject sender, EventArgs e) // ButtonClik event which delete selected row from Grid2 and add to Grid1

              {

              string hdnRowIndex = hdn4.Value.ToSt ring();tableGri d2.Rows.RemoveA t(int.Parse(hdn RowIndex));


              GridView2.DataS ource = tableGrid2;

              GridView2.DataB ind();

              GridView1.DataS ource = addNewRowGrid1( );

              GridView1.DataB ind();

              }

              protected DataTable addNewRowGrid1( ) // Function to add a row to Grid1

              {

              DataRow newRow =tableGrid1.New Row();string hdnRowData = hdn3.Value.ToSt ring();
              newRow[0] = hdnRowData;

              tableGrid1.Rows .Add(newRow);



              return tableGrid1;
              }

              protected void Button2_Click(o bject sender, EventArgs e) // ButtonClick event to delete all rows from Grid1 and add to Grid2

              {

              int r = tableGrid1.Rows .Count;while (r > 0)
              {

              DataRow newrow = tableGrid2.NewR ow();
              newrow[0] = tableGrid1.Rows[0].ItemArray.GetV alue(0).ToStrin g();

              if (tableGrid1.Row s[0].ItemArray.GetV alue(0).ToStrin g() != "")
              {

              tableGrid2.Rows .Add(newrow);

              }

              tableGrid1.Rows[0].Delete();

              r--;

              }

              if (tableGrid1.Row s.Count == 0)
              {

              tableGrid1.Rows .Add(tableGrid1 .NewRow());

              }

              GridView1.DataS ource =tableGrid1;

              GridView1.DataB ind();

              GridView2.DataS ource = tableGrid2;

              GridView2.DataB ind();

              }

              protected void Button4_Click(o bject sender, EventArgs e) // ButtonClick event to delete all rows from Grid2 and add to Grid1

              {

              int r = tableGrid2.Rows .Count;while (r > 0)
              {

              DataRow newrow =tableGrid1.New Row();
              newrow[0] = tableGrid2.Rows[0].ItemArray.GetV alue(0).ToStrin g();

              if (tableGrid2.Row s[0].ItemArray.GetV alue(0).ToStrin g() != "")
              {

              tableGrid1.Rows .Add(newrow);

              }

              tableGrid2.Rows[0].Delete();

              r--;

              }

              if (tableGrid2.Row s.Count == 0)
              {

              tableGrid2.Rows .Add(tableGrid2 .NewRow());

              }

              GridView2.DataS ource = tableGrid2;

              GridView2.DataB ind();

              GridView1.DataS ource = tableGrid1;

              GridView1.DataB ind();

              }

              }

              when i close n do start without debugging den no data is present in grid1.



              enable view state for grid1 is true only..

              Comment

              Working...