Table of TextBoxes isn't displayed as deisred - asp.net

I am adding texboxes into a table (type Table) but I can't add them. I can't add more than one cell to each row, any idea?
TextBox[] tx = new TextBox[10];
TableCell[] tc = new TableCell[10];
TableRow[] tr = new TableRow[10];
for (int i = 0; i < 10; i++)
{
tx[i] = new TextBox();
tc[i] = new TableCell();
tc[i].Controls.Add(tx[i]);
}
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
tr[i].Cells.Add(tc[i]);
}
for (int i = 0; i < 10; i++)
Table1.Rows.Add(tr[i]);
It comes out like 10 rows each having only 1 cell

Because you need an inner loop on this:
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
tr[i].Cells.Add(tc[i]);
}
Try this:
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
for (int x = 0; x < 10; x++)
{
tr[i].Cells.Add(tc[x]);
}
}

Your loops are not set up to give you a 10x10 table
Table table = new Table();
TableRow tr = null;
TableCell tc = null;
for (int i = 0; i < 10; i++)
{
tr = new TableRow();
for (int j = 0; j < 10; j++)
{
tc = new TableCell();
tc.Controls.Add(new TextBox());
tr.Cells.Add(tc);
}
table.Rows.Add(tr);
}

The cells has to be distinct: I need to create 100 cells not only 10!
TextBox[] tx = new TextBox[100];
TableCell[] tc = new TableCell[100];
TableRow[] tr = new TableRow[10];
for (int i = 0; i < 100; i++)
{
tx[i] = new TextBox();
tc[i] = new TableCell();
tc[i].Controls.Add(tx[i]);
}
int x = 0;
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
for (int j=0; j < 10; j++)
{
tr[i].Cells.Add(tc[x++]);
}
}
for (int i = 0; i < 10; i++)
Table1.Rows.Add(tr[i]);

Related

How to add Specific Border to Labels

I am making a Sudoku Solver in a JavaFx Application. I have got the grid ready as shown below, but I don't know how to add the center border to distinguish between the smaller boxes.
Like in a normal sudoku, I want to get the border between the smaller boxes.
Here's is the code:-
private GridPane getGridPane() {
GridPane gridPane = new GridPane();
gridPane.setGridLinesVisible(true);
gridPane.setPadding(new Insets(10));
gridPane.setHgap(0);
gridPane.setHgap(0);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
buttons[i][j] = new Label("");
buttons[i][j].setStyle("-fx-font-weight: bold; -fx-font-size: 40");
buttons[i][j].setAlignment(Pos.CENTER);
buttons[i][j].setTextAlignment(TextAlignment.CENTER);
buttons[i][j].setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}
...
for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) GridPane.setConstraints(buttons[i][j], j, i);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) gridPane.getChildren().add(buttons[i][j]);
GridPane.setHalignment(gridPane, HPos.CENTER);
GridPane.setValignment(gridPane, VPos.CENTER);
gridPane.setAlignment(Pos.CENTER);
return gridPane;
}

Semantic analyzer for checking type incompatibilities - Symbol table Implementation?

I got this code for Semanitic Analysis. Can't figure out the code for Symbol Table to make it run? Making a 2D array like this :
String[,] Symboltable = new String[20, 6];
Gives error of wrong number of indices inside, which I resolved but still wouldn't work. What is the other way I could do this?
Region Semantic Analyzer
void Semantic_Analysis(int k)
{
Regex variable_Reg = new Regex(#"^[A-Za-z|_][A-Za-z|0-9]*$");
if (finalArray[k].Equals("+"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String type = finalArray[k - 4] + "";
String left_side = finalArray[k - 3] + "";
int left_side_i = 0;
int left_side_j = 0;
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(left_side))
{ left_side_i = i; left_side_j = j; }
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (type.Equals(Symboltable[before_i][2]) && type.Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.Add(Ans);
}
if (Symboltable[left_side_i][2].Equals(Symboltable[before_i][2]) && Symboltable[left_side_i][2].Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.RemoveAt(Constants.Count - 1);
Constants.Add(Ans);
Symboltable[left_side_i][3] = Ans + "";
}
}
}
if (finalArray[k].Equals("-"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String type = finalArray[k - 4] + "";
String left_side = finalArray[k - 3] + "";
int left_side_i = 0;
int left_side_j = 0;
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(left_side))
{ left_side_i = i; left_side_j = j; }
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (type.Equals(Symboltable[before_i][2]) && type.Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) - Convert.ToInt32(Symboltable[after_i][3]);
Constants.Add(Ans);
}
if (Symboltable[left_side_i][2].Equals(Symboltable[before_i][2]) && Symboltable[left_side_i][2].Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.RemoveAt(Constants.Count - 1);
Constants.Add(Ans);
Symboltable[left_side_i][3] = Ans + "";
}
}
}
if (finalArray[k].Equals(">"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (Convert.ToInt32(Symboltable[before_i][3]) > Convert.ToInt32(Symboltable[after_i][3]))
{
int start_of_else = finalArray.IndexOf("else");
int end_of_else = finalArray.Count - 1;
for (int i = end_of_else; i >= start_of_else; i--)
{
if (finalArray[i].Equals("}"))
{
if (i < finalArray.Count - 2)
{ end_of_else = i; }
}
}
for (int i = start_of_else; i <= end_of_else; i++)
{ finalArray.RemoveAt(start_of_else); }
}
else
{
int start_of_if = finalArray.IndexOf("if");
int end_of_if = finalArray.IndexOf("}");
for (int i = start_of_if; i <= end_of_if; i++)
{ finalArray.RemoveAt(start_of_if); }
if_deleted = true;
}
}
}
}
#endregion

How to Export Multiple Datasets to Single ExcelSheet?

I am having two datasets in which first dataset has 6 rows and second dataset has 'N' Number of rows , second dataset row count keeps on changing , What I need is I need to export both the dataset to a single excelsheet . Both dataset has headers , So i need to export both dataset with header to a single excelsheet using c#. Can anyone help me with a sample code in c#
before u have to create
1. using Excel = Microsoft.Office.Interop.Excel;//in header,and Add correct refference
2. Excel.Application excelHandle1 = PrepareForExport(Ds); //add handle in calling function excelHandle1.Visible = true;
public Excel.Application PrepareForExport(System.Data.DataSet ds,string[] sheet)
{
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Add(missing);
DataTable dt1 = new DataTable();
dt1 = ds.Tables[0];
DataTable dt2 = new DataTable();
dt2 = ds.Tables[1];
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)excel.Worksheets.Add(missing, missing, missing, missing);
newWorksheet.Name ="Name of data sheet";
// for first datatable dt1..
int iCol1 = 0;
foreach (DataColumn c in dt1.Columns)
{
iCol1++;
excel.Cells[1, iCol1] = c.ColumnName;
}
int iRow1 = 0;
foreach (DataRow r in dt1.Rows)
{
iRow1++;
for (int i = 1; i < dt1.Columns.Count + 1; i++)
{
if (iRow1 == 1)
{
// Add the header the first time through
excel.Cells[iRow1, i] = dt1.Columns[i - 1].ColumnName;
}
excel.Cells[iRow1 + 1, i] = r[i - 1].ToString();
}
}
// for second datatable dt2..
int iCol2 = 0;
foreach (DataColumn c in dt2.Columns)
{
iCol2++;
excel.Cells[1, iCol] = c.ColumnName;
}
int iRow2 = 0;
foreach (DataRow r in dt2.Rows)
{
iRow2++;
for (int i = 1; i < dt2.Columns.Count + 1; i++)
{
if (iRow2 == 1)
{
// Add the header the first time through
excel.Cells[iRow2, i] = dt2.Columns[i - 1].ColumnName;
}
excel.Cells[iRow2 + 1, i] = r[i - 1].ToString();
}
}
return excel;
}
First you have to declare namespace:
using Excel = Microsoft.Office.Interop.Excel;
public static void Main(string[] args) {
DataTable dt1 = new DataTable("Employee");
dt1.Columns.Add("Employee ID");
dt1.Columns.Add("Employee Name");
dt1.Rows.Add("1", "ABC");
dt1.Rows.Add("2", "DEF");
dt1.Rows.Add("3", "PQR");
dt1.Rows.Add("4", "XYZ");
DataTable dt3 = new DataTable("Department");
dt3.Columns.Add("Department ID");
dt3.Columns.Add("Department Name");
dt3.Rows.Add("1", "IT");
dt3.Rows.Add("2", "HR");
dt3.Rows.Add("3", "Finance");
DataSet ds = new DataSet();
ds.Tables.Add(dt1);
ds.Tables.Add(dt3);
Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Excel._Worksheet worksheet = null;
app.Visible = false;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from gridview";
int z = 1;
for (int x = 0; x < ds.Tables.Count; x++)
{
int r = z;
int t = r + 1;
for (int i = 1; i < ds.Tables[x].Columns.Count + 1; i++)
{
worksheet.Cells[r, i] = ds.Tables[x].Columns[i - 1].Caption;
}
for (int i = 0; i < ds.Tables[x].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[x].Columns.Count; j++)
{
worksheet.Cells[i + t, j + 1] = ds.Tables[x].Rows[i]
[j].ToString();
}
}
z = ds.Tables[x].Rows.Count + 3;
}
string textPath = "e:\\output.xls";
if (File.Exists(textPath))
{
File.Delete(textPath);
}
workbook.SaveAs(textPath, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();
}

Filling data in grid view using textbox on button click -it show an a error inder out of bound

when i used a textbox to insert data it show index out of bound error
Button_Click()
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name"); ///enter code here
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;/// at this point
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
Try this
You have to use grd.Rows.Count-1 in for loop
Button_Click()
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
for (int intCnt = 0; intCnt < grd.Rows.Count-1; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;/// at this point
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
Hope this help you
change the condition of your for loop to be for (int intCnt = 0; intCnt < grd.Rows.Count-1; intCnt ++)
{...}
instead of for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{...}

HOw can I handle Dynamically generated Controls

In the following image the table displayed is generated dynamically on click of “Create Table” button.
I have added textboxes, fileupload, Buttons dynamically to the table.
I want to upload files from fileupload control on click of “Upload” button in the Table, but I have no idea how to handle those Dynamically generated Controls.
Code of “Create Table” button goes like this :
protected void Button1_Click(object sender, EventArgs e)
{
//Button1.Visible = false;
//Creat the Table and Add it to the Page
Table table = new Table();
table.Caption = "Table1";
table.BackColor = System.Drawing.Color.BurlyWood;
Page.Form.Controls.Add(table);
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
for (int j = 0; j <= 10; j++)
{
TableCell cell = new TableCell();
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + i + "Col" + j;
lbl.Text = "CC NO. " + i + " ";
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 1; j <= 1; j++)
{
Label lbl = new Label();
lbl.ID = "lblRow" + i + "Col" + j;
lbl.Width = 100;
lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 2; j <= 7; j++)
{
TextBox tb = new TextBox();
tb.Width = 100;
tb.ID = "txtBoxRow" + i + "Col" + j;
tb.Text = "";
// Add the control to the TableCell
cell.Controls.Add(tb);
}
for (j = 8; j <= 8; j++)
{
FileUpload fileUp = new FileUpload();
fileUp.ID = "flupRow" + i + "Col" + j;
fileUp.Width = 220;
cell.Controls.Add(fileUp);
}
for (j = 9; j <= 9; j++)
{
Button btnUpld = new Button();
btnUpld.Width = 100;
btnUpld.ID = "btnUpRow" + i + "Col" + j;
btnUpld.Text = "Upload";
cell.Controls.Add(btnUpld);
}
for (j = 10; j <= 10; j++)
{
Label lbl = new Label();
lbl.ID = "lblRow" + i + "Col" + j;
lbl.Text = "[ Status ]";
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
//table.Rows.Add(row);
}
Attach event handeler btnUpld runtime.
Button btnUpld = new Button();
btnUpld.Width = 100;
btnUpld.ID = "btnUpRow" + i + "Col" + j;
btnUpld.Text = "Upload";
btnUpld.Click += new EventHandler(btnUpld_Click);
cell.Controls.Add(btnUpld);
//code behind
private void btnUpld_Click(object sender, System.EventArgs e)
{
// Add upload functionality here
}
btnUpload.Click +=new EventHandler(btnUpload_Click);
add an event handler after adding the control dynamically to the form, using the above code and then add create a function that handles this event using the following code
protected void btnEdit_Click(object sender, EventArgs e)
{
}

Resources