DataGridView with horizontal columns - c#

Is it possible to have a horizontal columns in DataGridView, with ability to bind those columns?

You dont have to Flip the DataGridView instead Flip the DataSet to bind
Try this:
public DataSet FlipDataSet(DataSet my_DataSet)
{
DataSet ds = new DataSet();
foreach (DataTable dt in my_DataSet.Tables)
{
DataTable table = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{ table.Columns.Add(Convert.ToString(i)); }
DataRow r;
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
{ r[j] = dt.Rows[j - 1][k]; }
table.Rows.Add(r);
}
ds.Tables.Add(table);
}
return ds;
}
For more details visit Displaying-Vertical-Rows-in-DataGrid-View

Related

Generate Dynamic Rows & Columns with itemtemplate using Webform Gridview

I want to make data entry grid with validations and item template field which consist of multiple Webform controls. like SAP and Ax Dynamics data entry grid.
Kindly help me if you anyone have this kind of sample grid.
//You can store a DataTable in the session state
DataTable table = Session["Table1"] as DataTable;
table = new DataTable();
DataColumn colid = table.Columns.Add("sno", typeof(Int32));
DataColumn l_ID = table.Columns.Add("l_ID", typeof(String));
table.PrimaryKey = new DataColumn[] { colid };
colid.ReadOnly = true;
l_ID.ReadOnly = true;
getlist();
//List<Dictionary<string, string>> list = grid.GRCol();
for (int i = 0; i < list.Count; i++)
{
DataColumn dcol = new DataColumn(list[i]["fieldid"], typeof(string));
table.Columns.Add(dcol);
}
#region rowgenrate
for (int i = 1; i <= 1; i++)
{
DataRow aRow = table.NewRow();
for (int j = 0; j < table.Columns.Count; j++)
{
if (i == 1 && j > 1)
{
string abc = table.Columns[j].ToString();
aRow["sno"] = i;
aRow["l_ID"] = "001";
aRow[abc] = "";
//table.Rows.Add(aRow);
}
else
{
if (j > 1)
{
aRow["sno"] = i;
aRow["l_ID"] = "";
aRow[j] = "";
}`enter code here`
//table.Rows.Add(aRow);
}
}
table.Rows.Add(aRow);
}
#endregion
Session["Table1"] = table;

Query Excel where Rows and Columns are reversed

How can I query an Excel file where the rows and columns are reversed / rotated 90 degrees?
Can it be done with a SELECT query, or do I need to recurse the cells programmatically?
It's for a .NET app, so linq or other suggestions are welcome.
Transpose a Datatable with a code like this:
private DataTable GenerateTransposedTable(DataTable inputTable)
{
DataTable outputTable = new DataTable(inputTable.TableName);
outputTable.Columns.Add(inputTable.Columns[0].ColumnName);
foreach (DataRow inRow in inputTable.Rows)
{
string newColName = inRow[0].ToString();
outputTable.Columns.Add(newColName);
}
for (int rCount = 1; rCount <= inputTable.Columns.Count - 1; rCount++)
{
DataRow newRow = outputTable.NewRow();
newRow[0] = inputTable.Columns[rCount].ColumnName;
for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
{
string colValue = inputTable.Rows[cCount][rCount].ToString();
newRow[cCount + 1] = colValue;
}
outputTable.Rows.Add(newRow);
}
return outputTable;
}
.NET does not include a method to transpose data tables. You have to make your own. This website Link has a tutorial on an example transpose method. I will copy and paste the code snippet below:
private DataTable Transpose(DataTable dt)
{
DataTable dtNew = new DataTable();
//adding columns
for(int i=0; i<=dt.Rows.Count; i++)
{
dtNew.Columns.Add(i.ToString());
}
//Changing Column Captions:
dtNew.Columns[0].ColumnName = " ";
for(int i=0; i<dt.Rows.Count; i++)
{
//For dateTime columns use like below
dtNew.Columns[i+1].ColumnName =Convert.ToDateTime(dt.Rows[i].ItemArray[0].ToString()).ToString("MM/dd/yyyy");
//Else just assign the ItermArry[0] to the columnName prooperty
}
//Adding Row Data
for(int k=1; k<dt.Columns.Count; k++)
{
DataRow r = dtNew.NewRow();
r[0] = dt.Columns[k].ToString();
for(int j=1; j<=dt.Rows.Count; j++)
r[j] = dt.Rows[j-1][k];
dtNew.Rows.Add(r);
}
return dtNew;
}

I want to create the 'times table (2x2 = 4)' table using the GridView in Asp.net

I want to create a 9x9 GridView, list the numbers 1 through 9 in columns and rows, and then put the values ​​into an array.
I want to create it using for and Array, but I do not know what to do.
The current state is that the value is displayed below the column and all the values ​​appear only on one row.
I want to complete one program and help.
aspx.
<asp:GridView ID="GridView1" runat="server">
<Columns></Columns>
aspx.cs.
public partial class GridEX : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
int i = 1;
if (!Page.IsPostBack)
{
for (; i <= 9; i++)
{
dt.Columns.Add(i + "단");
}
for (int k = 1; k <= 9; k++)
{
DataRow dr = dt.NewRow();
for (int m = 1; m <= 9; m++)
{ dr[m] = i; }
dt.Rows.Add(dr);
dt.Rows.Add(i * k);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
What I want image:
DataTable dt = new DataTable();
dt.Columns.Add("Factor1");
dt.Columns.Add("Factor2");
dt.Columns.Add("Result");
for(int i=2; i<=9; i++)
{
for (int j=1; j<=10; j++)
{
dt.Rows.Add(i, j, i*j);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
You may have your reasons for wanting to use a Gridview here, but in my opinion, it is not the right way to achieve your goal. DataBinding is a useful tool but it does come with processing overhead, at the very least it is another iteration.
You can use a DataTable to achieve the same thing without the binding overhead.
ASPX
<asp:Table ID="MulitiplicationTable" runat="server"></asp:Table>
C#
for(int row = 0; row < 10; row++)
{
TableRow newRow = new TableRow();
MulitiplicationTable.Rows.Add(newRow);
for (int column = 0; column < 10; column++)
{
TableCell newCell = new TableCell();
newRow.Cells.Add(newCell);
//Empty cell for our first cell...
if ((row + column) == 0)
{
newCell.Text = " ";
}
//Our Column and row headers are also
//Special Cases
else if(row == 0)
{
newCell.Text = column.ToString();
newCell.CssClass = "columnHead";
}
else if(column == 0)
{
newCell.Text = row.ToString();
newCell.CssClass = "rowHead";
}
//Now do the math
else
{
newCell.Text = (row * column).ToString();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table9x9 = new DataTable();
if (table9x9.Columns.Count == 0)
{
for (int i = 0; i < 10; i++)
{
if (i == 0)
table9x9.Columns.Add(" ");
else
table9x9.Columns.Add(i+"");
}
}
for (int row = 1; row <= 9; row++)
{
DataRow dr = table9x9.NewRow();
for (int column = 0; column <= 9; column++)
{
if (column == 0)
dr[" "] = row;
else
dr[column + ""] = row * column;
}
table9x9.Rows.Add(dr);
}
GridView2.DataSource = table9x9;
GridView2.DataBind();
}
}
Output:

Show datasource rows as columns on a DataGridView (Winforms)

I wanna show all my DataSource rows on a DataGridView, but not as rows but as columns of a row. Each 12 items retrieved, I wanna insert a new row on the DataGridView and populate this new row with more items from the DataSource (12 at each row).
My DataSource retrieves just one item each, and using it directly with the DataGridView is working nicely, but shown a different row for each item.
Any hints?
Thanks to #SriramSakthivel.
private void AddToList(DataTable dt)
{
possibleWords = 0;
// Cleans the data grid view
WordList.DataSource = null;
WordList.Refresh();
// Let's transform the original data table onto another, changing rows by columns
DataTable table = new DataTable();
for (int i = 0; i < 10; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r;
int col = 0;
//for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
for (int j = 0; j < dt.Rows.Count; j++)
{
if (col >= 10)
{
table.Rows.Add(r);
col = 0;
r = table.NewRow();
}
r[col++] = (dt.Rows[j][0]).ToString().ToUpper();
possibleWords++;
}
table.Rows.Add(r);
}
// Puts the new data table as datasource of the word list
DataView dv = table.DefaultView;
WordList.DataSource = dv;
if (possibleWords == 0)
return;
WordList.Columns[0].DefaultCellStyle.BackColor = Color.WhiteSmoke;
WordList.ColumnHeadersVisible = false;
WordList.RowHeadersVisible = false;
}

Repost (sort of) -- Invalid Cast AGAIN (DataTable Row Summation)

I have the ability to total each column and add the row -- using this block of code :
DataRow totalRow = t.NewRow();
int colCount = 1;
for (int j = 1; j < t.Columns.Count; j++)
{
if (t.Columns[j].ColumnName == "Client")
{
t.Columns.Cast<DataColumn>().Skip(1);
}
else
{
int colTotal = 0;
for (int i = 1; i < t.Rows.Count; i++)
{
colTotal += Convert.ToInt32(t.Rows[i][j]);
totalRow[t.Columns[j].ColumnName] = colTotal;
}
}
++colCount;
}
t.Rows.Add(totalRow); <br>
**WHY O WHY Can't I just alter this OR use this block (below) to total the rows and insert a new column with the totals of each row??? I don't know why I'm having such a block on this--I'm sure it's relatively simple I just am not seeing it! It is driving me nuts -- I've been at this for 3 days --its sad.
int sum = 0;
foreach (DataRow rows in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
int number = Convert.ToInt32(dt.Rows[j].Field<int>(i));
sum += number;
}
}
rows["testrow"] = sum;
}
dataGridView1.DataSource = dt;
}
The error is still "Specified cast in not valid" -- The datatable is coming from an excel sheet. I can use it on a self-made DataTable just fine. I don't understand.
This block of code works just fine and gives me the sum of the rows in a new column
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("amount1", typeof(int));
dt.Columns.Add("amount2", typeof(int));
dt.Columns.Add("amount3", typeof(int));
dt.Columns.Add("amount4", typeof(int));
dt.Columns.Add("Row Totals", typeof(int));
DataRow dr = dt.NewRow();
dr[0] = 100;
dr[1] = 200;
dr[2] = 300;
dr[3] = 400;
dr[4] = 0;
dt.Rows.Add(dr);
int sum = 0;
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
// int sum = 0;
int number = dt.Rows[j].Field<int>(i);
sum += number;
}
}
It seems likely that it's not an int but a long (or byte) you can cast a long to int but you can't unbox a long to an int which the code in question tries to do.
if that's the case you can do something like
var sum = (from column in dt.Columns.AsEnumerable<DataColum>().Skip(1)
from row in dt.Rows.AsEnumerable<DataRow>().Skip(1)
where column.ColumnName != "Client"
select (long)row[column]).Sum();
Drum roll please ---
Thank you guys for comments and suggestions they did help me get to this point understanding what exactly was going on behind the scenes.
System.Data.DataTable dt = ds.Tables[0];
dt.Columns.Add("testrow", typeof(int));
DataRow dr = dt.NewRow();
int sum = 0;
for (int i = 1; i < dt.Columns.Count; i++)
{
for (int j = 1; j < dt.Rows.Count; j++)
{
if (j == dt.Rows.Count - 1)
{
dt.Rows[i][j] = Convert.ToInt32(sum);
sum = 0;
}
else
{
object number = dt.Rows[i][j];
sum += Convert.ToInt32(number);
}
}
dataGridView1.DataSource = dt;
}

Categories

Resources