How to dynamically add gridviews side by side using asp.net c# - c#

I am creating a page which shows some details in several gridviews. I am saying several because the number of gridviews is not constant. I am having a panel on the aspx page and adding gridviews to it dynamically.
aspx code:
<asp:Panel ID="pnlResult" runat="server"></asp:Panel>
aspx.cs code
int numOfGroups = some number here;
for (int i = 1; i < numOfGroups + 1; i++)
{
GridView grd = new GridView();
grd.ID = "GridView" + i.ToString();
grd.BackColor = getColor(i);
grd.DataSource = dt; // some data source
grd.DataBind();
pnlResult.Controls.Add(grd);
}
But my problem is that the gridviews are adding one below the another . I want them to be side by side. How can I achieve that?
Note: Panel is not mandatory. Anything else can be used in its place

You have to float your elements inside the panel to left. To achieve that in your code behind, before adding grid to the panel do:
grd.Attributes.Add("class", "float-left");
Where float-left class in your css is defined as:
.float-left {
float: left;
}
So your code would look like:
for (int i = 1; i < numOfGroups + 1; i++)
{
GridView grd = new GridView();
grd.ID = "GridView" + i.ToString();
grd.BackColor = getColor(i);
grd.Attributes.Add("class", "float-left"); //here
grd.DataSource = dt; // some data source
grd.DataBind();
pnlResult.Controls.Add(grd);
}

If you use a table control, then you can create a row, and add each grid as cells, which would enforce the side-by-side requirement:
TableCell cell = new TableCell();
cell.Controls.Add(grd);
table.Rows(0).Cells.Add(cell);
Something like that; not sure if the Table API example above is 100% correct, but hopefully you get the idea.

Maybe u need just to add some css?
like that:
grd.DataSource = dt; // some data source
grd.Style["float"] = "left"; // css
grd.DataBind();

You can do something like this
Table tbl = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
btn = new Button();
btn.Text = "Add ";
btn.Height = Unit.Pixel(30);
btn.Width = Unit.Pixel(100);
tc.Controls.Add(btn);
tr.Controls.Add(tc);
tbl.Controls.Add(tr);
panel.Controls.Add(tbl);

Related

C# datagridviewbuttoncolumn no text showing

I need to show some texts on the DataGridViewButtonColumn. I've read lots of similar question about these on SO. Many of them answers recommend setting UseColumnTextForButtonValueto True which doesn't for me. It seems in an odd way that Microsoft makes it to have at least a row so that the button will display the text.
The following is my code:
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.HeaderText = "Complete";
EditColumn.Name = "Complete";
EditColumn.UseColumnTextForButtonValue = True;
dataGridView.Columns.Add(EditColumn);
This code doesn not show the text on the DataGridViewButtonColumn:
The code that works here:
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "a";
dataGridView1.Columns[1].Name = "b";
ArrayList row = new ArrayList();
row.Add("1");
row.Add("2");
dataGridView1.Rows.Add(row.ToArray());
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "text";
btn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(btn);
This code works but however I am getting my data from a database and thus I won't be using dataGridView.Rows.Add(row.ToArray()) here. So how do I get the text to show on the DataGridViewButtonColumn ?
You have two problems to solve:
How bind a Button column to a DataSource
How to make the DataSource create not only default column types but also one (or more) button column
The first issue is basically solved by setting the DataPropertyName of the column.
But I do not know how or even if it is possible to influence the ColumnTypes created automatically.
Therefore I suggest a workaround: Instead of relying on the AutoGenerateColumns we can do it ourselves in a little helper function:
This clears the columns and creates a new one for each column in a DataTable, using the properties of the columns in the table. To tell it which column(s) we want to be buttons we pass a string with the column names, surrounded by spaces:
void CreateSpecialColumns(DataTable dt, DataGridView dgv, string buttons)
{
dgv.Columns.Clear();
for (int i = 0; i < dt.Columns.Count; i++ )
{
DataColumn dc = dt.Columns[i];
if (buttons.Contains(" " + dc.ColumnName + " "))
{
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = dc.Caption;
buttonColumn.Name = dc.ColumnName;
buttonColumn.ValueType = dc.DataType;
buttonColumn.DataPropertyName = dc.ColumnName;
dgv.Columns.Add(buttonColumn);
}
else
{ // normal columns
int c = dgv.Columns.Add(dc.ColumnName, dc.Caption);
dgv.Columns[c].ValueType = dc.DataType;
dgv.Columns[c].DataPropertyName = dc.ColumnName;
}
}
}
Here is how I called it using a DataTable DT:
dataGridView1.AutoGenerateColumns = false;
CreateSpecialColumns(DT, dataGridView1, " Cook Waiter ", "");
dataGridView1.DataSource = DT;
Note that this only works if the UseColumnTextForButtonValue property is not set to true!
You need to set the Text property,
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.HeaderText = "Complete";
EditColumn.Name = "Complete";
EditColumn.UseColumnTextForButtonValue = True;
EditColumn.Text = "Complete"; // --Text property added
dataGridView.Columns.Add(EditColumn);

Find Dynamically Created Gridviews that is inside a masterpage and inside a collapsible panels

This has definitely been a 1 step forward 43 steps back. Started by creating by first gridview user control. But anyway. What I have is dynamically created gridviews. The entire page is built on masterpage and each gridview resides inside its own dynamically created collapsible panel.
Now in I need to loop through each gridview's rows. Problem is I cannot find them in the code behind. I have tried various recursive functions with no luck. So I have no clue to what I am doing with this.
Here is the code I am using to create the gridviews and panels.
int x = 1;
while (sqlReader.Read())
{
// Create Panel Header
Panel PH = new Panel();
PH.ID = "PH" + Convert.ToString(x);
PH.CssClass = "cheader";
// Add Label inside header panel to display text
Label lblPH = new Label();
lblPH.ID = "lblPH" + Convert.ToString(x);
lblPH.Text = sqlReader["Category"].ToString();
PH.Controls.Add(lblPH);
//Create Body Panel
Panel PB = new Panel();
PB.ID = "PB" + Convert.ToString(x);
// Create CollapsiblePanelExtender
CollapsiblePanelExtender CPE = new CollapsiblePanelExtender();
CPE.ID = "CPE" + Convert.ToString(x);
CPE.TextLabelID = lblPH.ID;
CPE.ExpandControlID = PH.ID;
CPE.CollapseControlID = PH.ID;
CPE.TargetControlID = PB.ID;
CPE.ScrollContents = false;
CPE.Collapsed = true;
CPE.ExpandDirection = CollapsiblePanelExpandDirection.Vertical;
CPE.SuppressPostBack = true;
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(PH);
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(PB);
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(CPE);
//*** This is loading the gridview user control
EPOGVSurvey ctl = Page.LoadControl("EPOGVSurvey.ascx") as EPOGVSurvey;
ctl.MyParameterValue = sqlReader["Category"].ToString();
ctl.ID = "CAT" + x;
PB.Controls.Add(ctl);
PB.CssClass = "cdata";
x++;
}
sqlReader.Close();
Code to try to read the gridviews
GridView gv = FindControlRecursive(Page, "Gridview" + i) as GridView;
if (gv != null)
{
foreach (GridViewRow row in gv.Rows)
{

how to add textboxes dynamically to a table row in C#

i want to add textboxes dynamically in C# on a button click in a table row. for that i have used the following code.
[1]: http://pastie.org/7702237.
the problem is i am able to adding as many textboxes as I want but they are adding at same location, i mean the table row is not incrementing for every button click instead it is simply replacing the old table row with the new one. Please help me in how to solve this problem.
Thanks in advance
Ganesh
Web-based applications do not maintain state; to this end the state of the table and any variables is not being maintained. With each postback (generated by the button), the table's state reverts to what it was prior to adding a row and then a row is programatically added to it.
In order to achieve your goal, you will need to maintain state somehow. In the following code snippet I am making use of a session:
private List<TableRow> TableRows
{
get
{
if(Session["TableRows"] == null)
Session["TableRows"] = new List<TableRow>();
return (List<TableRow>)Session["TableRows"];
}
}
The following is your code modified to work with the session variable:
TextBox txtE, txtM, txtB;
Button btnAdd, btnDel;
TableRow trow;
TableCell tcell;
foreach(TableRow tr in TableRows)
tblEduDetails.Controls.Add(tr);
int count = TableRows.Count + 1;
txtE = new TextBox();
txtE.ID = "E" + count.ToString();
txtE.Visible = true;
txtE.Text = "E " + count.ToString();
txtE.BorderWidth = 2;
txtE.TextMode = TextBoxMode.SingleLine;
txtE.Height = 30;
txtM = new TextBox();
txtM.ID = "M" + count.ToString();
txtM.Visible = true;
txtM.Text = "M " + count.ToString();
txtM.TextMode = TextBoxMode.SingleLine;
txtM.Height = 30;
txtB = new TextBox();
txtB.ID = "E" + count.ToString();
txtB.Visible = true;
txtB.Text = "B " + count.ToString();
txtB.TextMode = TextBoxMode.SingleLine;
txtB.Height = 30;
btnAdd = new Button();
btnAdd.ID = "A" + count.ToString();
btnDel = new Button();
btnDel.ID = "D" + count.ToString();
trow = new TableRow();
trow.ID = "R" + count.ToString();
trow.BorderWidth = 1;
tcell = new TableCell();
tcell.ID = "E" + count.ToString();
tcell.Controls.Add(txtE);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "B" + count.ToString();
tcell.Controls.Add(txtM);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "M" + count.ToString();
tcell.Controls.Add(txtB);
trow.Controls.Add(tcell);
tblEduDetails.Controls.Add(trow);
TableRows.Add(trow);

Add textbox via dynamic imagebutton in a panel

I have a problem I seem to stumble over all the time, I have a Drop Down box and you can select a number which creates x number of textboxes with images buttons its for a survey it the image buttons are used to create "Sub-Answers" so they can have answers to answers so my question is I need to when they hit the image button to create a textbox under the orginal textbox here is the code.
for (Int32 i = 1; i <= NumberOfAnwsers; i++)
{
Literal l1 = new Literal();
l1.Text = "<tr><td>Answer " + i + " text.</td><td>";
TextBox tb = new TextBox();
tb.ID = "TextBoxAnswer" + i;
tb.EnableViewState = false;
tb.Width = 300;
Literal l3 = new Literal();
l3.Text = "</td><td>";
Literal l2 = new Literal();
l2.Text = "</td></tr>";
RadColorPicker CPI = new RadColorPicker();
CPI.PaletteModes = PaletteModes.WebPalette;
CPI.ID = "RadColorPicker" + i;
CPI.ShowIcon = true;
CPI.SelectedColor = System.Drawing.Color.Black;
ImageButton IBVideo = new ImageButton();
IBVideo.ID = "IBVideo" + i;
IBVideo.ImageUrl = "/images/video-icon.jpg";
IBVideo.ToolTip = "Add Video";
IBVideo.Height = 20;
IBVideo.Width = 20;
ImageButton IBAdd = new ImageButton();
IBAdd.ID = "IBAdd" + i;
IBAdd.ImageUrl = "/images/add-icon.png";
IBAdd.ToolTip = "Add Sub-Answers";
//IBAdd.OnClientClick = "showDialog(" + i + ");return false;";
IBAdd.Height = 20;
IBAdd.Width = 20;
//Add Textbox
PanelAnswersToQuestions.Controls.Add(l1);
PanelAnswersToQuestions.Controls.Add(tb);
PanelAnswersToQuestions.Controls.Add(l3);
PanelAnswersToQuestions.Controls.Add(CPI);
PanelAnswersToQuestions.Controls.Add(IBVideo);
PanelAnswersToQuestions.Controls.Add(IBAdd);
PanelAnswersToQuestions.Controls.Add(l2);
}
As you can see I just add controls to the panel, I need to know when that ImageBUtton is hit I can add a Textbox and in this case it could be more then just one textbox to it.
I hope this is clear but for some reason I dont think it is ... sorry.
I have added a radwindow and poping that up sending the Data to the partent via javascript the which created a new problem for me, I can not in javascript seem to find the dynamicly created hiddenfield
function OnClientClose(radWindow) {
var oWnd = $find("<%=RadWindowAddSubAnswer.ClientID%>");
var SubAnswerValues = oWnd.get_contentFrame().contentWindow.document.forms(0).HiddenFieldSubAnswers.value;
alert(SubAnswerValues);
var AnswerID = oWnd.get_contentFrame().contentWindow.document.forms(0).HiddenFieldAnswerID.value;
alert(AnswerID);
var HiddenName = "HiddenFieldSubAnswers" + AnswerID;
alert(HiddenName);
document.getElementById(HiddenName).value = SubAnswerValues;
$get("DivSubAnswers" + AnswerID).innerHTML = SubAnswerValues;
}
The "document.getElementById(HiddenName).value = SubAnswerValues;" seems to never be found, I also tried $get(HiddenName).value = SubAnswerValues; that does not seem to work either both come back as null as for the code behind its:
HiddenField HFSubAnswers = new HiddenField();
HFSubAnswers.ID = "HiddenFieldSubAnswers" + i;
HFSubAnswers.Value = "0";
Im not sure if I got your question right but if you need to dynamically add controls on a Page here is what I can say.
Before adding your control I guess you need to find the control where you need to add it on, Add the control then assign the properties.
PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
myPlaceHolder.Controls.Add(myButton);
myButton.Text = "Hello World";
For a more detailed expalnation go here http://anyrest.wordpress.com/2010/04/06/dynamically-removing-controls-in-a-parent-page-from-a-child-control/

Controls.remove() method not working in asp.net

I have a web app where the user can create dynamic textboxes at run time. When the user clicks SUBMIT, the form sends data to the database and I want remove the dynamic controls.
The controls are created in the following code:
Table tb = new Table();
tb.ID = "tbl";
for (i = 0; i < myCount; i += 1)
{
TableRow tr = new TableRow();
TextBox txtEmplName = new TextBox();
TextBox txtEmplEmail = new TextBox();
TextBox txtEmplPhone = new TextBox();
TextBox txtEmplPosition = new TextBox();
TextBox txtEmplOfficeID = new TextBox();
txtEmplName.ID = "txtEmplName" + i.ToString();
txtEmplEmail.ID = "txtEmplEmail" + i.ToString();
txtEmplPhone.ID = "txtEmplPhone" + i.ToString();
txtEmplPosition.ID = "txtEmplPosition" + i.ToString();
txtEmplOfficeID.ID = "txtEmplOfficeID" + i.ToString();
tr.Cells.Add(tc);
tb.Rows.Add(tr);
}
Panel1.Controls.Add(tb);
The Remove section of the code is:
Table t = (Table)Page.FindControl("Panel1").FindControl("tbl");
foreach (TableRow tr in t.Rows)
{
for (i = 1; i < myCount; i += 1)
{
string txtEmplName = "txtEmplName" + i;
tr.Controls.Remove(t.FindControl(txtEmplName));
string txtEmplEmail = "txtEmplEmail" + i;
tr.Controls.Remove(t.FindControl(txtEmplEmail));
string txtEmplPhone = "txtEmplPhone" + i;
tr.Controls.Remove(t.FindControl(txtEmplPhone));
string txtEmplPosition = "txtEmplPosition" + i;
tr.Controls.Remove(t.FindControl(txtEmplPosition));
string txtEmplOfficeID = "txtEmplOfficeID" + i;
tr.Controls.Remove(t.FindControl(txtEmplOfficeID));
}
}
However, the textboxes are still visible.
Any ideas?
I would assume that your creating these controls each time the page is loaded or else when your remove code ran on postback it would fail because they would not exist.
So you need to move the create code to NOT happen with every page load by making sure it is wrapped in a if (!IsPostBacK) { ... } statement.
If you do this then you wouldn't need to remove them manually since they are created dynamically and therefore not created by default on each postback.
If you can post where the code that creates the controls is being called from, and the remove as well, I could help you a bit more.
What I ended up doing was instead of deleting the textbox, I deleted the TableRow;

Categories

Resources