Generate Dynamic Rows & Columns with itemtemplate using Webform Gridview - c#

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;

Related

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:

Number looping get row number(01,02,03)

string[] SName = Request.Form.GetValues("Description");
string[] Email = Request.Form.GetValues("Email");
DataTable dtable = dt();
for (int i = 0; i <= SName.Length - 1; i++)
{
DataRow row1 = dtable.NewRow();
row1["Description"] = SName[i];
row1["Email"] = Email[i];
DAL.DMSS insertdata = new DMSS();
insertdata.INSERT_DATA(loggeduser.SUBSIDIARY_CD, input, SName[i], Email[i]);
}
above are my code to get the data from dynamic row add.
if i have 2 rows,data i get is :
now i want to add 1 more data ,sequence number
tried this code but not working..
for (int i = 0; i <= SName.Length - 1; i++)
{
if (i.length <2 )
{
string strvalue = i.PadLeft(2, '0');
}
else
{
string strvalue = i;
}
DataRow row1 = dtable.NewRow();
row1["Description"] = SName[i];
row1["Email"] = Email[i];
DAL.DMSS insertdata = new DMSS();
insertdata.INSERT_DATA(loggeduser.SUBSIDIARY_CD, input, SName[i], Email[i], strvalue);
}
for (int i = 0; i <= SName.Length - 1; i++)
{
var rowNumber = (i + 1).ToString("0#");
}

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;
}

Union tables of dataset in a single datatable

I have a DataSet with many similar tables in it. I need to union all datatables in a single table. So for this example; I need a datatable with 20 rows. How can I do that?
DataSet SampleDS = new DataSet();
DataTable SampleTbl1 = new DataTable();
SampleTbl1.Columns.Add("Product", typeof(string));
SampleTbl1.Columns.Add("Value", typeof(int));
for (int i = 0; i < 10; i++)
{
SampleTbl1.Rows.Add("Product " + i, i);
}
SampleDS.Tables.Add(SampleTbl1);
DataTable SampleTbl2 = new DataTable();
SampleTbl2.Columns.Add("Product", typeof(string));
SampleTbl2.Columns.Add("Value", typeof(int));
for (int i = 0; i < 10; i++)
{
SampleTbl2.Rows.Add("Product " + i, i*2);
}
SampleDS.Tables.Add(SampleTbl2);
You can make a new DataTable, then import rows of two tables into new table.
DataTable t = new DataTable();
t.Columns.Add("Product", typeof(string));
t.Columns.Add("Value", typeof(int));
foreach(DataTable table in SampleDS.Tables)
{
if(table != null && table.Rows.Count > 0)
{
for(int i = 0; i < table.Rows.Count; i ++)
t.ImportRow(table.Rows[i]);
}
}
HTH.
Try with LINQ
var table = SampleDS.Tables[0].AsEnumerable();
for (var ii = 1; ii < SampleDS.Tables.Count; ii++) {
table = table.Concat(SampleDS.Tables[ii].AsEnumerable());
}

Categories

Resources