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.
Related
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
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);
}
Hi Can any one suggest how to read data from datagrid in windowsforms application which has two columns(FileName and FilePath).
Below is the code I tried its returning all Filename and FilePath in single column(FileName).
Any suggestions would be helpful to me..
`
public System.Data.DataTable ExportToExcel()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("FileName", typeof(string));
table.Columns.Add("FilePath", typeof(string));
for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
{
for (int col= 0; col < dataGridView1.Rows[rows].Cells.Count; col++)
{
table.Rows.Add(dataGridView1.Rows[rows].Cells[col].Value.ToString());
}
}
The issue is that the line…
table.Rows.Add(dataGridView1.Rows[rows].Cells[col].Value.ToString());
is only adding to the FIRST column. You are adding a new row for EACH column.
To add a row to a table that has two columns would be of the form…
table.Rows.Add(column1Value, column2Value);
Since you know there are TWO columns, simply loop through the rows. It is not necessary to loop through the columns.
DataTable table = new DataTable();
table.Columns.Add("FileName", typeof(string));
table.Columns.Add("FilePath", typeof(string));
for (int rows = 0; rows < dataGridView1.Rows.Count; rows++) {
if (!dataGridView1.Rows[rows].IsNewRow &&
dataGridView1.Rows[rows].Cells[0].Value != null &&
dataGridView1.Rows[rows].Cells[1].Value != null) {
table.Rows.Add(dataGridView1.Rows[rows].Cells[0].Value.ToString(), dataGridView1.Rows[rows].Cells[1].Value.ToString());
}
}
If it is unknown how many columns are in the DataGridView and you must loop through the columns… then you will need to make sure there are at least as many columns in the DataGridView as there are columns in the DataTable.
DataTable table = new DataTable();
table.Columns.Add("FileName", typeof(string));
table.Columns.Add("FilePath", typeof(string));
DataRow newRow;
if (dataGridView1.Columns.Count >= table.Columns.Count) {
for (int row = 0; row < dataGridView1.Rows.Count; row++) {
if (!dataGridView1.Rows[row].IsNewRow) {
newRow = table.NewRow();
for (int col = 0; col < table.Columns.Count; col++) {
if (dataGridView1.Rows[row].Cells[col].Value != null)
newRow[col] = dataGridView1.Rows[row].Cells[col].Value.ToString();
else
newRow[col] = "";
}
table.Rows.Add(newRow);
}
}
}
If you know what object dataGridView1.DataSource is, just visit the object. It would be much easier.
Besides, there's a simple way to export data from DataGridView to Excel:
Ctrl+A Select all cells in DataGridView.
Ctrl+C Copy them.
Ctrl+V Paste them in a sheet of Excel.
I have successfully created generating new records into table rows but want to add table header, so how to do that?
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
You do it like that:
TableHeaderRow header = new TableHeaderRow(); // Creating a header row
table.Rows.Add(header); // Add the header row to table tbl
For adding cells in the header row here is an example:
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
And this is where is goes in your code:
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
TableHeaderRow header = new TableHeaderRow();
table.Rows.Add(header);
//These two lines in iteir own loop
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
Building on the suggested solution by #israel-altar, I struggled with adding the header into a <thead> tag and not into a <tbody>.
To achieve this, you have to use the TableSelection property of your TableHeaderRow:
private void GenerateTable(List<string> fieldsList)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
TableHeaderRow header = new TableHeaderRow();
foreach (string field in fieldsList)
{
TableHeaderCell headerTableCell1 = new TableHeaderCell();
headerTableCell1.Controls.Add(new Literal() {
Text = field
});
header.Cells.Add(headerTableCell1);
}
// Set TableSection to TableHeader so that the TableHeaderRow is added within <thead> and not within <tbody>
header.TableSection = TableRowSection.TableHeader;
table.Rows.Add(header);
// loop rows ...
}
I am trying to copy data from my datagridview to a datatable so i can export to a csv file
here is the code
public DataTable createdatatablefromdgv()
{
DataTable dsptable = new DataTable();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
DataColumn dspcolumn = new DataColumn(dataGridView1.Columns[i].HeaderText);
dsptable.Columns.Add(dspcolumn);
}
int noOfColumns = dataGridView1.Columns.Count;
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
DataRow dataRow = dsptable.NewRow();
for (int i = 0; i < noOfColumns; i++)
{
dataRow[i] = dgvr.Cells[i].Value.ToString();
}
}
return dsptable;
}
It seems like it copies the data from the the grid to to the table but when I return the datatable all there is the columns no rows
You are not adding dataRow to data table after assigning values to its columns.
public DataTable createdatatablefromdgv()
{
DataTable dsptable = new DataTable();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
DataColumn dspcolumn = new DataColumn(dataGridView1.Columns[i].HeaderText);
dsptable.Columns.Add(dspcolumn);
}
int noOfColumns = dataGridView1.Columns.Count;
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
DataRow dataRow = dsptable.NewRow();
for (int i = 0; i < noOfColumns; i++)
{
dataRow[i] = dgvr.Cells[i].Value.ToString();
}
dsptable.Rows.Add(dataRow); //Add this statement to add rows to Data Table
}
return dsptable;
}
The above answer is correct but a little explanation as to why you encountered this problem. One would think that by calling NewRow() on the DataTable you would add a new row to the DataTable and then be able to access it.
What NewRow actually does is give you an instance of a DataRow which you can then use column names as apose to column identifiers (integers).
This allows you to do something like this
DataRow dataRow = dsptable.NewRow();
foreach(DataColumn dc in dsptable.Columns)
{
dataRow[dc.ColumnName] = dgvr.Cells[dc.ColumnName].Value.ToString()
}
Alternatively you could just call Rows.Add() which takes an object array or a as you were trying to do a DataRow.
List<string> rowData = new List<string>();
for (int i = 0; i < noOfColumns; i++)
{
rowData.Add(dgvr.Cells[i].Value.ToString());
}
dsptable.Rows.Add(rowData.ToArray());
This should explain adding rows to a datatable more simply :)