GridViewRow Headers placed above each other - c#

I am working on ASP.NET C#. I am populating gridview with data from database. There are columns to the GridView that is been bound with DataTable. I ant to group these columns with Header Rows. I tried to add these Header Rows to a GridView as the following (Sample code):
I tried manipulating with row index but nothing happened.
protected void AddHeaders()
{
GridViewRow vesselInfoRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell vesselInfotc = new TableCell();
vesselInfotc.Text = "Vessel Information ";
vesselInfotc.Style.Add("text-align", "center");
vesselInfotc.Style.Add("font-size", "medium");
vesselInfotc.Style.Add("font-weight", "bold");
vesselInfotc.Style.Add("background-color", "#b7dee8");
vesselInfotc.Style.Add("color", "black");
vesselInfotc.ColumnSpan = 4;
vesselInfoRow.Cells.Add(vesselInfotc);
this.BerthScoreCardGridView.Controls[0].Controls.AddAt(0, vesselInfoRow);
//
GridViewRow vesselArrivalRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell vesselArrivaltc = new TableCell();
vesselArrivaltc.Text = "Vessel Arrival ";
vesselArrivaltc.Style.Add("text-align", "center");
vesselArrivaltc.Style.Add("font-size", "medium");
vesselArrivaltc.Style.Add("font-weight", "bold");
vesselArrivaltc.Style.Add("background-color", "#b7dee8");
vesselArrivaltc.Style.Add("color", "black");
vesselArrivaltc.ColumnSpan = 2;
vesselArrivalRow.Cells.Add(vesselArrivaltc);
this.BerthScoreCardGridView.Controls[0].Controls.AddAt(0, vesselArrivalRow);
}
However the rows are placed above each other as the following:
How can i place them horizontally next to each other. Appreciate your help.

You should add all the cells to the same row and add that single row to the GridView:
protected void AddHeaders()
{
GridViewRow topHeaderRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell vesselInfotc = new TableCell();
vesselInfotc.Text = "Vessel Information ";
...
vesselInfotc.ColumnSpan = 4;
topHeaderRow.Cells.Add(vesselInfotc);
TableCell vesselArrivaltc = new TableCell();
vesselArrivaltc.Text = "Vessel Arrival ";
...
vesselArrivaltc.ColumnSpan = 2;
topHeaderRow.Cells.Add(vesselArrivaltc);
// Add the other cells here
...
BerthScoreCardGridView.Controls[0].Controls.AddAt(0, topHeaderRow);
}
And call your function in the PreRender event of the GridView:
void BerthScoreCardGridView_PreRender(object sender, EventArgs e)
{
AddHeaders();
}

Related

How to include html elements inside a webcontrol such as a TableCell in C#

I am writing a code in webforms to add rows to a table after a button click. and inside some cells i need add html elements like textbox and dropdows,
protected void LinkButton1_Click(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "<input type=\"text\"></input>";
row.Cells.Add(cell1);
//proptable is the table id
PropTable.Rows.Add(row);
}
it works if i write like this.
but is there a more accurate way to do this?
any help is appreciated.
You can add Controls to TextBox easily like this:
protected void LinkButton1_Click(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TextBox textbox = new TextBox();
textbox.Text = "Testing...";
//cell1.Text = "<input type=\"text\"></input>";
cell1.Controls.Add(textbox);
row.Cells.Add(cell1);
//proptable is the table id
PropTable.Rows.Add(row);
}

Expand/Collapse dynamic GridViewRow in asp.net c#

I am dynamically creating a GridViewRow in RowCreated event of gridview and I need to expand/collapse this particular row alone on button click.
GridViewRow HeaderGridRow1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell1 = new TableCell();
TableCell HeaderCellEC = new TableCell();
ImageButton imgEC = new ImageButton();
imgEC.ImageUrl = "Images.png";
imgEC.ID = "imgShowHide";
imgEC.Click += ImgShowHide_Click;
HeaderCellEC.Controls.Add(imgEC);
HeaderGridRow1.Cells.Add(HeaderCellEC);
HeaderCell1.Text = "Info";
HeaderGridRow1.Cells.Add(HeaderCell1);
GridView1.Controls[0].Controls.AddAt(1, HeaderGridRow1);
Code Behind:
private void ImgShowHide_Click(object sender, ImageClickEventArgs e)
{
}
Now in ImgShowHide_Click event I need to perform expand/collapse on HeaderGridRow1. Since its dynamic control how can I achieve it.

How to retrieve data from tablecell ASP.NET

When I load the page I send the number of rows the table will have:
protected void Page_Load(object sender, EventArgs e)
{
string numFilas = Request.QueryString["filas"];
tblAdd.Visible = true;
for (int i = 0; i < int.Parse(numFilas); i++)
{
TableRow NewRow1 = new TableRow();
TableCell NewCell1 = new TableCell();
TableCell NewCell2 = new TableCell();
TextBox txtBox1 = new TextBox();
txtBox1.Width = 200;
TextBox txtBox2 = new TextBox();
txtBox2.Width = 200;
// adding lebel into cell
NewCell1.Controls.Add(txtBox1);
NewCell2.Controls.Add(txtBox2);
// adding cells to row
NewRow1.Cells.Add(NewCell1);
NewRow1.Cells.Add(NewCell2);
tblAdd.Rows.Add(NewRow1);
}
}
now when I click submit I'd like to retrieve the data of the textbox inside the table, what I've been capable of is this:
public void submit(Object sender, EventArgs e)
{
for (Int32 i = 0; i < tblAdd.Rows.Count; i++)
{
TableRow dr = tblAdd.Rows[i];
TableCell hhh = dr.Cells[0];
String textCell = hhh.Text();
}
}
However, the text in the cells is empty because the text the user writes is IN the textbox, which I don't know how to get it.
Try this
While creating your text boxes add an ID
E.g.
txtBox1.ID = "txtBox1";
Then you can easily find this TextBox control from the current Cell's Controls collection as follows.
string textCell = ((TextBox)dr.Cells[0].FindControl("txtBox1")).Text;
Hope you understood what I'm trying to say. Basically, you need to find your control in the Cell.
Vote and accept the answer if it solved your issue.
Cheers!

making dynamic created dropdownlist public to other classes

I have the following code:
foreach (ListItem item in check.Items)
{
if (item.Selected)
{
TableRow row = new TableRow();
row.Style.Add("color", "white");
TableCell cell = new TableCell();
cell.Style.Add("background-color", "blue");
Label lbl = new Label();
lbl.Text = item.Text;
cell.Controls.Add(lbl);
row.Cells.Add(cell);
TableCell cell1 = new TableCell();
cell1.Style.Add("background-color", "green");
DropDownList drop = new DropDownList();
drop.Style.Add("align", "right");
drop.Items.Add(" ");
drop.Items.Add("1");
drop.Items.Add("2");
drop.DataValueField = "0";
drop.DataValueField = "1";
drop.DataValueField = "2";
cell1.Controls.Add(drop);
row.Cells.Add(cell1);
this.TblCheck.Rows.Add(row);
drop.SelectedIndexChanged += new EventHandler(drop_SelectedIndexChanged);
drop.AutoPostBack = true;
}
}
}
private void drop_SelectedIndexChanged(object sender,EventArgs e)
{
if (drop.SelectedValue == "1")
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Style.Add("background-color", "blue");
Label lbl = new Label();
lbl.Text = "H1";
cell.Controls.Add(lbl);
row.Cells.Add(cell);
this.tabel1.Rows.Add(row);
}
if (drop.SelectedValue == "2")
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Style.Add("background-color", "blue");
Label lbl = new Label();
lbl.Text = "H1";
cell.Controls.Add(lbl);
row.Cells.Add(cell);
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Style.Add("background-color", "blue");
Label lbl1 = new Label();
lbl1.Text = "H2";
cell1.Controls.Add(lbl1);
row1.Cells.Add(cell1);
this.tabel1.Rows.Add(row);
this.tabel1.Rows.Add(row1);
}
}
I have a drop down list and when I select an option for each option selected a row is created which contains the selected value and another column with a dropdownlist.I want for that drop down list created dynamic to add a selectedindexchanged.I ask how to make reference in my selectedindexchanged to the dynamic created dropdownlist whici is named "drop".I tried to make it public but than my table isn't created well.It's an as.net web application using c#.
In the SelectedIndexChanged event you have access to the object which triggered the event. In your case it would be the dynamically created control to which you associated the event. You can type cast that as a DropDownList within the event like so:
protected void drop_SelectedIndexChanged(object sender, EventArgs e)
{
var drop = (DropDownList)sender;
if (drop.SelectedValue == "1")
{
...
}
}

Binding HyperLinks to GridView using AutoGenerateColumns

I have a DataTable which has columns that are generated dynamically at runtime. This DataTable is bound to a GridView that has AutoGenerateColumns set to true. I've run into a problem with this since some of the data in the DataTable are HyperLink objects, so instead of displaying the actual link in the table, it displays "System.Web.UI.WebControls.HyperLink".
Normally, I would just use a HyperLinkField in my GridView, but since the GridView's columns are generated automatically, I'm not sure how to do this. Any ideas?
You can add a new column dynamically, you'll just have to hide the automatically generated columns also.
For this solution you can either store the hyperlink in 2 columns - 1 for the link and 1 for the text you want displayed, or if you want something generic displayed (like 'Click Here') you can just store the link (eg "http://example.com.au/Default.aspx").
protected void GridView1_RowBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//add new header
TableCell tc = new TableCell();
tc.Text = "Groovy Link";
e.Row.Cells.Add(tc);
//hide original column that has been autobound - skip column we just added
for (int i = 0; i < e.Row.Cells.Count - 1; i++)
{
BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
if (field.DataField == "AutoGeneratedColumnName")
field.Visible = false;
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
//create new tablecell
TableCell tc = new TableCell();
//do a check to see if the data is stored as a hyperlink in DB
if (DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString().StartsWith("<a") == true)
{
//create hyperlink
HyperLink hyp = new HyperLink();
hyp.NavigateUrl = DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString();
hyp.Text = "click here";
tc.Controls.Add(hyp);
}
else
{
//just text
tc.Text = DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString()
}
//add tablecell to row
e.Row.Cells.Add(tc);
//hide original column that has been autobound
for (int i = 0; i < e.Row.Cells.Count - 1; i++)
{
BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
if (field.DataField == "AutoGeneratedColumnName")
field.Visible = false;
}
}
}

Categories

Resources