Setting the textarea in a GridView after DataBind - c#

How can I set the textarea of the gridview which has a Datatable:
<asp:GridView ID="gvTemplateFields" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Risk">
<ItemTemplate>
<textarea id="Risk"
cols="20" rows="2"
runat="server"
style="width: 99%">
</textarea>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>No off-site links found.</EmptyDataTemplate>
</asp:GridView>
The data binding:
DataTable templateFields = SWMSField.GetTemplateFields(TemplateId);
gvTemplateFields.DataSource = templateFields;
gvTemplateFields.DataBind();

You can use an ASP.NET server control instead:-
<asp:TemplateField HeaderText="Risk">
<ItemTemplate>
<asp:TextBox id="Risk" TextMode="MultiLine" Columns="20" Rows="2" runat="server"
Text='<%# Eval("RiskColumn")%>' />
</ItemTemplate>
</asp:TemplateField>
Here, RiskColumn is the column name which holds the data you want to bind in textarea.

Related

Unable to maintain data inside gridview across postback when used inside an user control

I've a textbox that is present inside a GridView that itself is a part of user control and I'm dynamically loading this user control on page_Init.
The controls in my user control are a DropDown, a TextBox and a GridView.
Now DropDown and TextBox are able to retain values across postback but textboxes inside the GridView are not retaining the values.
Adding User Control on page init:
if (postBackCntrl.Contains("AddUserControlButton"))
{
UserControl newGrid = (UserControl)LoadControl("~/UserControl.ascx");
newGrid.ID = "test" + gridList.Count;
gridList.Add(newGrid);
}
GridView Code: TextBoxes inside GridView ItemTemplate not able to retain values across postback:
<asp:GridView runat="server" ID="grdUser" CssClass="GridTable" Width="90%" AutoGenerateColumns="false"
ShowFooter="true" OnDataBound="MergeGridViewFooter">
<Columns>
<asp:TemplateField HeaderText="ContactUs">
<ItemTemplate>
<asp:TextBox ID="txtContactUs" runat="server" CssClass="textbox" Height="60px" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnaddrow" runat="server" Text="Add Row" CssClass="ui-button jquery-ui" Style="font-size: 12px" OnClick="btnaddrow_Click" />
<asp:Button ID="btnDelRowAddress" runat="server" Text="Delete Row" CssClass="ui-button jquery-ui" Style="font-size: 12px" OnClick="btnDelRowAddress_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Definition">
<ItemTemplate>
<asp:TextBox ID="txtDefinition" runat="server" TextMode="MultiLine" Height="60px" CssClass="textbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Height="60px" CssClass="textbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

how to wrap the data into multi line in grid in asp.net

I bind my grid to database. My grid displays the large string in a single line but I want to display the string in new line if it is too lengthy. I tried all possibilities but nothing works. please tell me where I have made mistake.
Thanks in advance.
Here is my code
<asp:TemplateField HeaderText="Procedure_Name" ItemStyle-VerticalAlign="Top">
<ItemStyle Wrap="true" Width="200px" />
<ItemTemplate>
<%# Eval("Procedure_Name").ToString().Replace(Environment.NewLine,"<br/>")%>
</ItemTemplate>
</asp:TemplateField>
Try below,
<%# Eval("Procedure_Name").ToString().Replace(vbcrlf,"<br/>")%>
Edited,
<asp:TemplateField HeaderText="Procedure_Name" ItemStyle-VerticalAlign="Top" ItemStyle-Width="600">
<ItemStyle Wrap="true" Width="200px" />
<ItemTemplate>
<div style="width:800px; overflow: scroll;">
<asp:Label ID="lbl_target" runat="server" Text='<%# Bind("Procedure_Name") %>' CssClass="paraGraphtext" Width="800" ></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>

How to arrange datatable columns inside gridview?

My gridview is having a template field bound from the aspx designer. I'm binding a data table to it. Now my template field, which is having few action buttons, is coming as the first column. Is there any way to arrange the datatable columns before the template field?
Code from Designer for the GridView:
<asp:GridView ID="JobListGrid" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<div>
<asp:ImageButton ID="View" CssClass="imgbutton" ToolTip="View Pdf" runat="server"
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' ImageUrl="~/Content/pdf.PNG" CommandName="View" Width="36" Height="36" OnClientClick='<%# Eval("JobID", "OpenInNewWindow(\"{0}\").ToString()") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS Code:
JobListGrid.DataSource = dataTableObj;
JobListGrid.DataBind();
The above code shows the grid view headers like :
TemplateField | Col1 | Col2 | Col3
I need the Templatefield to come last. The col1, col, col3 are getting from the datatable.
Change your GridView like this, for controlling columns you AutoGenerateColumns must be disable.
<asp:GridView ID="JobListGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="JobID" HeaderText="JobID" />
<asp:BoundField DataField="JobName" HeaderText="Name" />
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<div>
<asp:ImageButton ID="View" CssClass="imgbutton" ToolTip="View Pdf" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' ImageUrl="~/Content/pdf.PNG" CommandName="View" Width="36" Height="36" OnClientClick='<%# Eval("JobID", "OpenInNewWindow(\"{0}\").ToString()") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
var columns = JobListGrid.Columns.CloneFields(); //Will get all the columns bound dynamically to the gridview.
var columnToMove = JobListGrid.Columns[0]; //My first column is Action Column
JobListGrid.Columns.RemoveAt(0); // Remove it
JobListGrid.Columns.Insert(columns.Count - 1, columnToMove); // Moved to last
JobListGrid.DataBind(); // Bind the grid .
This thing worked for me.
You have to use a template field for each column that you want from your datatable. Use a label inside your template field for displaying text using <%#Bind("yourColumnName")%> for text property. That way, you can arrangecolumns in any order you wish for. Also set autogenerate columns to false in gridview. Something like
<asp:GridView ID="JobListGrid" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="myDataTableColumn1">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server"
Text='<%# Bind("yourDataTableColumnName") %>'></asp:Label>
<ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions">
<Columns>
<asp:ImageButton ID="View" CssClass="imgbutton" ToolTip="View Pdf" runat="server"
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' ImageUrl="~/Content/pdf.PNG" CommandName="View" Width="36" Height="36" OnClientClick='<%# Eval("JobID", "OpenInNewWindow(\"{0}\").ToString()") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I have binded a grid with List<int> (temporary list), how can i pick the list text into controls text in item template in grid view

I have binded a grid with List (temporary list),
how can I pick the list text into controls text in item template in grid view.
aspx
<asp:GridView ID="gvDenomination" runat="server">
<Columns>
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<asp:Label ID="lblValue" runat="server" Text='<%#Eval("lstdenomination") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CodeBehind
stdenomination.Add(Convert.ToInt32(txtDenomination.Text));
gvDenomination.DataSource = lstdenomination;
gvDenomination.DataBind();
If I get you, lstdenomination is a List<int>. You can do this
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<asp:Label ID="lblValue" runat="server" Text='<%# Container.DataItem %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>

Need idea to design my grid. asp.net(c#) gridview

My DataGrid contain totally 32 columns.I need to display my first 4 column as default column.
For the remaining Column i need to view using scroll bar.(not Page scroll need scroll bar inside my grid view)
When I've had to do this, what I did was create 2 Gridviews next to each other
The first Gridview should have only the freezable columns. The second will have the rest, and will be in a scrollable div tag.
I had defined amount of of rows, so it wasn't a problem for me, but if you scroll down on one grid, the rows won't match the second grid.
Here's another solution using CSS..but I haven't tried it:
http://www.codeproject.com/KB/webforms/FreezePaneDatagrid.aspx
If you use third party controls like the Telerik RadGrid, they usually have built in properties to control Freezing Columns.
Try this
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="Column 1" />
<asp:BoundField HeaderText="Column 2" />
<asp:BoundField HeaderText="Column 3" />
<asp:BoundField HeaderText="Column 4" />
<asp:TemplateField>
<ItemTemplate>
<div style="overflow: scroll; width: 400px;">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Column 5") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Column 6") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Column 7") %>'></asp:Label>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Column 8") %>'></asp:Label>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Column 9") %>'></asp:Label>
....
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Column 32") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Categories

Resources