• 0

[C#] Table Control


Question

HI,

can anybody help me fixing the asp code

TableRow Tr = new TableRow();
TableCell Tc = new TableCell();
Tc.ColumnSpan=1;
Tc.Text= "Test";
Tr.Cells.Add(Tc);
Table1.Rows.Add(Tr);

this code build the table with some data "that an example"; <--- :)

i have put that code in a button but whatever i click it

it like redraw the whole grid.....i wanted to add a row on each button click is this possible??

thanks in advance

note: if you know how to put data programitly in a gird on each button click that can help.

Link to comment
https://www.neowin.net/forum/topic/248058-c-table-control/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

i'm trying to do a sheet for a game (the game isn't copmuter based). so when click happen i need to put the game score in the gird. so the gird will be filled by each game finished in the real world.we put the number and the program calculate the score and put it in the girdt

i hope u got what i need.

thanks in advance

  • 0

You need to store your table and your data set in Session, ViewState, or some other state mechanism, like the global cache. Then you can track what rows have been added, and where your data is at.

Here's a really lame example to get you off to a start. I'm using Session to manage my page state.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestAspNet
{
	/// &lt;summary&gt;
	/// Summary description for WebForm1.
	/// &lt;/summary&gt;
	public class WebForm1 : Page
	{
  protected System.Web.UI.WebControls.Button btnAddRow;
  protected System.Web.UI.WebControls.PlaceHolder tblPH;
  protected System.Web.UI.WebControls.Table tblMain;
  protected int mCounter;

  private void Page_Load(object sender, EventArgs e)
  {
 	 // Put user code to initialize the page here
 	 if( Page.IsPostBack == false )
    InitFrm();
  }

  private void InitFrm()
  {
 	 tblMain = new Table();
 	 TableRow row = new TableRow();
 	 TableCell col = new TableCell();
 	 tblPH.Controls.Add(tblMain);

 	 tblMain.Rows.Add(row);
 	 row.Cells.Add(col);
 	 col.Text = "Row 1";

 	 mCounter = 1;
 	 Session.Add("counterounter);
 	 Session.Add("tablelMain);
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
 	 //
 	 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
 	 //
 	 InitializeComponent();
 	 base.OnInit(e);
  }
  
  /// &lt;summary&gt;
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// &lt;/summary&gt;
  private void InitializeComponent()
  {    
 	 this.btnAddRow.Click += new System.EventHandler(this.btnAddRow_Click);
 	 this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btnAddRow_Click(object sender, System.EventArgs e)
  {
 	 TableRow row;
 	 TableCell col;
 	 tblMain = (Table)Session["table"];
 	 mCounter = (int)Session["counter"];
 	 for( int i = mCounter; i &lt; mCounter + 1; i++ )
 	 {
    row = new TableRow();
    col = new TableCell();
    tblMain.Rows.Add(row);
    row.Cells.Add(col);
    col.Text = "Row " + (i + 1).ToString();
 	 }
 	 mCounter++;
 	 Session["counter"] = mCounter;
 	 tblPH.Controls.Add(tblMain);
  }
	}
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.