How to add Table data from cs file to aspx.cs file? - c#

I don't know how I could connect these two files to work.
output.cs file
public static void PrintGrid(Grid grid)
{
for (int j = 0; j <= grid.GetHeight(); j++)
{
TableRow row = new TableRow();
for (int i = 0; i <= grid.GetWidth(); i++)
{
TableCell cell = new TableCell();
cell.Text = grid.Get(i, j);
row.Cells.Add(cell);
}
Table1.Rows.Add(row);
}
}
Form1.aspx.cs file
protected void Button1_Click(object sender, EventArgs e)
{
//other code
InOut.PrintGrid(grid);
}
I tested it and wrote it in Button1_Click class ant it works fine, but in some way I need to write this fragment in another cs file and print this table in aspx.

Well, your static class does NOT have Table1 defined.
So, say this:
public static class GridOutPut
{
public static void PrintGrid(GridView grid, DataTable MyTable)
{
for (int j = 0; j <= grid.Rows.Count; j++)
{
TableRow row = new TableRow();
for (int i = 0; i <= grid.Columns.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = grid.Rows[i].Cells[j].Text;
row.Cells.Add(cell);
}
MyTable.Rows.Add(row);
}
}
}
(and don't use "output", as that is far to common of a name - and output used for all kind of things!!).
So, now your code say is like this:
DataTable dt = new DataTable();
// dt.Columns.Add() or whatever.
GridOutPut.PrintGrid(GridView1, dt);
So, pass the grid AND the table to that routine.
Of course the above is "air code", but it should suffice for the approach here.
So, you need to pass both the grid variable (instance), (I don't know what you using), and then the table

Related

way to put linkbutton into tablecell, asp.net

Here is the part of a code which builds DB from SQL query, but I need to put extra cell for LinkButton.
for (int j = 0; j < numrows; j++)
{
TableRow row = new TableRow();
for (int i = 0; i < numcols; i++)
{
TableCell cell = new TableCell();
string textContent;
textContent = subjects.Rows[j][i].ToString();
cell.Text = textContent;
row.Cells.Add(cell);
}
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
row.Cells.Add(cell1);
table.Rows.Add(row);
}
The question is that I don't know what do to with this part of a code where I put button into cell, I couldn't find a way to do it. Is it possible to do it without GridView, because I can't use it?
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
Here you go, a simple example. But remember to not add the table to the page within IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do not add dynamic controls like the linkbutton in the table in "IsPostBack"
}
//to generate some random data
var random = new Random();
//number of rows and cells
var rowCount = random.Next(5, 25);
var cellCount = random.Next(3, 10);
//create a new table
var table = new Table()
{
CssClass = "table table-striped"
};
//add the rows
for (int i = 0; i < rowCount; i++)
{
var row = new TableRow();
//add the cells
for (int j = 0; j < cellCount; j++)
{
//create a new cell
var cell = new TableCell();
//if the cell is not the last add content, otherwise add the linkbutton
if (j < cellCount - 1)
{
cell.Text = "CellValue " + random.Next(1000, 100000).ToString();
}
else
{
//create a new linkbutton
var lb = new LinkButton()
{
Text = "LinkButton"
};
//add the click event handler
lb.Click += new EventHandler(LinkButton_Click);
//add the linkbutton to the cell
cell.Controls.Add(lb);
}
//add the cell to the row
row.Cells.Add(cell);
}
//add the row to the table
table.Rows.Add(row);
}
//add the table to the page
PlaceHolder1.Controls.Add(table);
}

How to create nested html table in c# using recursion logic?

I just tried over it but i got the HTML tables in the manner of unique,i just need the tables to created one within another.
sample output: http://i.stack.imgur.com/drdXx.jpg][1]sample
In Design Page
<table id="tableContent" border="1" runat="server"></table>
protected void Page_Load(object sender, EventArgs e)
{
var s = 0;
Addtable(s);
}
public void Addtable(int j)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = "col 1";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "col2";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
tableContent.Border =1;
if( j < 5)
{
j++;
Addtable(j);
}
}
You need to change your recursion function as below
public void Addtable(HtmlTable baseTable, int j)
{
HtmlTable innerTable = new HtmlTable();
// change to stylesheet instead. Just added as an example to get the output
baseTable.Style.Add("margin-left", "25px");
baseTable.Style.Add("margin-right", "25px");
baseTable.Style.Add("margin-bottom", "25px");
baseTable.Style.Add("text-align", "center");
baseTable.Border = 1;
//Create a container cell for inner table
HtmlTableRow container = new HtmlTableRow();
HtmlTableCell containerCell = new HtmlTableCell();
Literal l = new Literal();
l.Text = "Table " + j;
containerCell.Controls.Add(l);
containerCell.Controls.Add(innerTable);
containerCell.ColSpan = 2;
container.Cells.Add(containerCell);
baseTable.Rows.Add(container);
if (j < 5)
{
j++;
Addtable(innerTable, j);
}
}
Then call the function
var s = 0;
Addtable(tableContent,s);

Properties in code built table in asp.net

I'm doing an exercise on asp.net using code generated tables with a very simple code:
protected void btnAceptar_Click(object sender, EventArgs e)
{
tblGenerar.Controls.Clear();
for(int i = 0; i < Convert.ToInt32(txtRows.Text);i++)
{
TableRow rowNew = new TableRow();
tblGenerar.Rows.Add(rowNew);
for (int j = 0; j < Convert.ToInt32(txtCols.Text);j++ )
{
TableCell cellNew = new TableCell();
rowNew.Cells.Add(cellNew);
cellNew.Text = txtTexto.Text;
if (chkMargen.Checked == true)
{
cellNew.BorderStyle = BorderStyle.Inset;
cellNew.BorderWidth = 1;
}
}
}
}
The first time I choose to create border on the table, it works, but next time I choose to generate the table without the borders, the borders from last generated table are still there. Additional cells appears with no borders.
Why does this happen if I'm using Controls.Clear() and how can I solve it?
Thanks.
Put else condition within your code.
else
{
cellNew.BorderStyle = BorderStyle.None;
cellNew.BorderWidth = 0;
}
or you can do something like following.
cellNew.BorderStyle = BorderStyle.None;
if (chkMargen.Checked == true)
{
cellNew.BorderStyle = BorderStyle.Inset;
cellNew.BorderWidth = 1;
}
And you are done.
This is because once your Table is generated you can not apply changes on them and to do that you need to explicitly remove border first and then apply if check box is checked.

how to verify if my datatable is empty

hello again y have populate my datatable using 1 button an array, next button 2 is going to send my datatable to excel, so what a i do is this using C#, and asp.net:
out side of the button y declare my datable so i can be use in both buttons
System.Data.DataTable _myDataTable =new System.Data.DataTable();
button 1 fill datatable: for this example lets say only 10 columns, caract=number of cells
for (int k=0; k < 10; k++)
{
_myDataTable.Columns.Add();
}
for (int j=0; j < 10; j++)
{
TableRow r = new TableRow();
System.Data.DataRow row=_myDataTable.NewRow();
for (int i = 0; i < caract+1; i++)
{
row[i]=(datar[j,i].ToString());
}
_myDataTable.Rows.Add(row);
Table1.Rows.Add(r);
}
now button 2 allows to the user if he wants to save the data from the datatable to an excel, but first i verifi if the datable is empty
if(_myDataTable !=null || _myDataTable.Rows.Count == 0)
{
string name="productos";
Label2.Text="it has data";
}
else{Label2.Text="NO data"; }
no i recibe the text that i have data yupi for me, but when i press button 2 to send the datatable to excel, it creats the document however is empty,
so the next thing i would like to try is to verifi cell by cell if it has the data is supposed to have, but only have no idea how to extract the data from the datatable and to be display in a label.
I appreciate any help
Your if condition looks wrong on a couple of points.
This is how I would code it:
if(_myDataTable !=null && _myDataTable.Rows.Count > 0)
The above means - if _myDataTable is a valid object and it contains values.
JUAN you can define your DataTable in global scope. Then you can use it in both of your button event handlers.
For example:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private DataTable _myDataTable;
public DataTable MyDataTable {
get
{
return _myDataTable;
}
set
{
_myDataTable = value;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int k=0; k < 10; k++)
{
MyDataTable.Columns.Add();
}
for (int j=0; j < 10; j++)
{
TableRow r = new TableRow();
System.Data.DataRow row = MyDataTable.NewRow();
for (int i = 0; i < caract+1; i++)
{
row[i]=(datar[j,i].ToString());
}
MyDataTable.Rows.Add(row);
Table1.Rows.Add(r);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if(MyDataTable !=null || MyDataTable.Rows.Count == 0)
{
string name="productos";
Label2.Text="it has data";
}
else
{
Label2.Text="NO data";
}
}
}
}
I know this is an old question but there is an easy way to solve this right now as follows:
if ((dataTableName?.Rows?.Count ?? 0) > 0)

Convert DataRow to TableRow

Simple enough. Cant find a simple solution. Not sure if their is a simple solution? :/
I have a dataTable. I essentially want to achieve this...
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
TableRow tr = new TableRow();
tr = dt.Rows[0];
but i cant convert from DataRow to a TableRow!
Help!
Alex
A TableRow is a UI element, it is represents a row within a Table control). The DataRow is a data element, it represents a row of data within a DataTable. You can't convert between the two like this because they are simply different beasts.
If you're looking to work with the UI then you should bind the Table control to the DataTable. Then you can pull the individual TableRow objects.
What are you trying to do?
if your goal is to show on the UI the data that are within a dataTable(or any other datasource) why don't you use a repeater?
anyway you can't just convert the DataTableRow to TableRow but you have to do it yourself.
have a look at the below code
private void GenerateTable()
{
DataTable dt = CreateDataTable();
Table table = new Table();
TableRow row = null;
//Add the Headers
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.Text = dt.Columns[j].ColumnName;
row.Cells.Add(headerCell);
}
table.Rows.Add(row);
//Add the Column values
for (int i = 0; i < dt.Rows.Count; i++)
{
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = dt.Rows[i][j].ToString();
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
// Add the the Table in the Form
form1.Controls.Add(table);
}
source:
http://geekswithblogs.net/dotNETvinz/archive/2009/06/24/fill-asp.net-table-with-data-from-datatable.aspx
you can use this
TableRow tr = new TableRow();
tr .Cells(0).Text = dt.Rows[0][0];
because TabeRow have so many cell, you must have to specified cell. there is no method to add whole datarow in to TableRow both are Different.

Categories