When I hide a field in one of my DataGrid columns, the column's width appears to be omitted from the grid altogether, causing the sizing of all my other columns to be readjusted. Now the sizing of the column is already being auto-adjusted [responsively] by the bootstrap styling (CssClass="table table-hover"; see below). That's fine. What's not fine is the column width being adjusted a second time when I dynamically (from code-behind) hide one of the fields (Visible = false)--reason being, I have a second DataGrid identical to this first one below whose untouched column widths must match (I'm not hiding any fields there).
<asp:datagrid id="dgS" AutoGenerateColumns="False" CssClass="table table-hover" ShowHeader="False" ShowFooter="False" runat="server" DataKeyField="SID" CellPadding="0" GridLines="None" AllowSorting="True" OnItemDataBound="dgS_ItemDataBound">
<HeaderStyle CssClass="" BackColor="steelblue" ForeColor="white" />
<Columns>
<asp:TemplateColumn HeaderText="SID" SortExpression="SID" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:Label ID="lblSID" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade" SortExpression="GradeChangeDescription" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:dropdownlist id="ddlG" Runat="server"></asp:dropdownlist>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="C" Visible="False" HeaderStyle-CssClass="" ItemStyle-CssClass="">
<ItemTemplate>
<asp:Label id="lblC" Runat="server" text='<%# String.Format("{0:MM/dd/yyyy}", DataBinder.Eval(Container.DataItem, "C"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Instead of hiding/removing the column all together and cause resize you could hide its content with opacity. eg:
.fakeHidden { opacity: 0;}
I am getting data from MSSQL in ASP.NET using gridview. The data is coming successfully. Only two columns that has HTML in it and have longer text is not showing properly. Each cell ends after 33 characters.
I am getting the data and putting it in LABEL.
The code is as follows inside gridview
<asp:GridView ID="GridView1" runat="server" Width="100%"
Font-Names="Arial" Font-Size="Small"
AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="IDLessonLearnt" CellPadding="3" CellSpacing="3"
DataSourceID="SqlDataSource1" PageSize="25" ShowFooter="True">
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#25a0da" Font-Names="Arial" Font-Size="14px" Height="30px"></HeaderStyle>
<Columns>
<asp:TemplateField ItemStyle-Width="600px" HeaderText="LESSONS / ISSUES" ItemStyle-HorizontalAlign="left">
<ItemTemplate>
<asp:Label ID="LessonLearned" runat="server" Text ='<%# System.Web.HttpUtility.HtmlEncode(Eval("LessonsIssues"))%>' ></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="true" Width="600" />
<HeaderTemplate>
<asp:TextBox ID="txtLessonLearned" runat="server" onkeyup="filter_LessonLearned(this)" CssClass="texbox_header"
Width="590px" placeholder="LESSONS / ISSUES" Text=""> </asp:TextBox>
</HeaderTemplate>
<ItemStyle HorizontalAlign="left" Width="600px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
I am not using any programming this Gridview and Data source are in .aspx file. All the data is coming fine even html code in database is getting parsed but it is only showing 33 characters of the cell.
Is there any limitation in it? Any suggestions.
Thxn in advance
I found out that the table had varchar(max) but the stored procedure i was using had the length of the columns to 200 which was developed by another developer. I changed to 9000 and now it is working.
Basically, what I want to do is to bind a Linq query to the datasource of a gridview. Here is my code so far :
ddgDossiers.DataSource = (From c In dbConnection.Campaigns.AsQueryable Select c).ToList
ddgDossiers.DataBind()
It compiles and doesn't complain of anything.
<asp:GridView ID="ddgDossiers" runat="server" CellPadding="0" CellSpacing="0" AllowSorting="True"
AutoGenerateColumns="False" EnableViewState="True" AllowPaging="True" PageSize="35"
PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" PagerStyle-Position="TopAndBottom"
BorderWidth="1" GridLines="Both" BorderColor="#000000" CssClass="mGrid" PagerStyle-CssClass="pgr">
<PagerSettings Position="TopAndBottom" />
<Columns>
<asp:TemplateField Visible="false" HeaderText="UniqueID" ItemStyle-HorizontalAlign="left"
HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="1%" HeaderStyle-Width="1%"
HeaderStyle-CssClass="GridTitle" ItemStyle-CssClass="rowReport" SortExpression="UniqueId">
<ItemTemplate>
<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The error is pointing on this line :
<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("CampaignId").ToString%>' />
And saying : No default member found for type 'Campaign'.
Is there anything to do with that? Thanks in advance.
change line
Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>'
To
Text='<%# DataBinder.Eval(Container.DataItem, "CampaignId") %>'
My GridView is DataBound to a SQL data connection which should almost always return data. So using an EmptyDataTemplate does not help me because the GridView should never be empty. But I want the first few rows to be editable so the user can add new information to the GridView. So I've crafted my Select statement to always come back with 3 empty rows. I want those rows to contain TextBoxes instead of Labels. But this:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
BorderColor="White" BorderStyle="Solid"
onselectedindexchanged="GridView1_SelectedIndexChanged" ShowFooter="False"
ViewStateMode="Disabled">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="checkbox" id ="CheckBox1" class="checkbox" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial" SortExpression="Serial">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Serial") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Model") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="From Store">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropdownlist"
DataSourceID="SqlDataSource2" DataTextField="Store"
SelectedValue='<%# Bind("Store") %>'>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="To Store">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="dropdownlist"
DataSourceID="SqlDataSource2" DataTextField="Store">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle HorizontalAlign="Center" ForeColor="White" BorderColor="White" BorderStyle="Solid" />
<HeaderStyle ForeColor="White" BorderColor="White" BorderStyle="Solid" />
<RowStyle BorderColor="White" BorderStyle="Solid" ForeColor="White" />
</asp:GridView>
Produces this:
Where the first 3 rows have uneditable Labels rather than TextBoxes. Is what I want to do possible?
In your template fields add <FooterTemplate></FooterTemplate> This makes the footer row of your gridview a place where you can add new rows. Of course you will need to put items inside the <FooterTemplate>, but the work the same as your <ItemTemplates
So to serve my specific purpose, I decided just to create a table separate from this with an empty row with textboxes. Then a button that used JQuery to take the values from that table to append them into the uneditable row in the DataGrid. Removed the header row from the DataGrid so it all looked like the same table.
I thought this would be simple, but I sure am having a lot of trouble doing this:
The title of this question may be a bit misleading. I don't have to use a gridview. In fact, I know the GridView is probably not the way to go on this. I just didn't know how else to title it. But, for now just consider:
I have a very simple class called Student. It has 4 properties:
int ID
string FirstName
string LastName
string Email
I want to keep a generic collection of these in memory (session state):
List students;
Ok, now the problem:
I want the user to create as many of these Student objects as they want. For displaying these I just want a simple table of some kind with the 3 textboxes on each row. I would like every row to have textboxes insead of labels so that any record can be edited at anytime.
When the user is finished created their student objects, then they proceed on to do other things. But, I am just having trouble finding a way to display the records this way. Do I use the ListView(3.5), html table, gridview, repeater, etc.?
How would you do it?
I would be inclined to use the ListView personally for this, since you can insert Rows with it. Your LayoutTemplate would be a table with a <tr runat="server" ID="itemPlaceHolder" /> in it. Your ItemTemplate would have your TextBox's (and optional a save button per row. Then you could have an InsertItemTemplate if you need inserts as well.
Anywhere on the page you can add a button to Save all items by looping through the ListView.Item collection and calling ListView.Update(itemIndex, validate).
<asp:ListView runat="server" ID="lv" InsertItemPosition="LastItem" DataKeyNames="id">
<LayoutTemplate>
<asp:LinkButton runat="server" OnClick="SaveAll" Text="Save All" />
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<tr runat="server" id="itemPlaceHolder" />
</table>
<asp:LinkButton runat="server" OnClick="SaveAll" Text="Save All" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("firstName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("lastName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("email") %>' /></td>
<td><asp:LinkButton runat="server" CommandName="update" Text="Save" /></td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("firstName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("lastName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("email") %>' /></td>
<td><asp:LinkButton runat="server" CommandName="insert" Text="Save" /></td>
</tr>
</InsertItemTemplate>
</asp:ListView>
protected void SaveAll(object sender, EventArgs e)
{
lv.Items.ToList().ForEach(li => lv.UpdateItem(li.DataItemIndex, true)_;
}
Yes you could use a grid view to do this.
you could create a custom template for the columns so that they display in a textbox as opposed to a label. you can then capture the 'save' event an loop through the rows and update your data.
Gridview is going to be the easiest way to implement this. You could use an html table, but when the user wants to add more users you're going to have to do a lot more. Create a template for the gridview with your four properties (Id, FirstName, LastName, Email), and then just bind it from the session object like:
public void BindGrid()
{
// assume students is the name of your GridView control
students.DataSource = (List<Student>)Session["StudentList"];
students.DataBind();
}
Yes, this is possible. This is just a code sample for what Victor had posted.
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="PartNumber" GridLines="Horizontal"
ForeColor="#333333" CellPadding="4" Width="800" PageSize="20" OnDataBound="gvPartDetails_DataBound" OnSelectedIndexChanged="gvPartDetails_SelectedIndexChanged" OnSorting="gvPartDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server" Text='<%# Bind("Number") %>' ToolTip='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOC 1">
<ItemTemplate>
<asp:TextBox ID="txt_Qty1" AutoPostBack="false" runat="server" MaxLength="5" Width="50" Text='<%# Bind("Qty1") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOC 2">
<ItemTemplate>
<asp:TextBox ID="txt_Qty2" AutoPostBack="false" runat="server" MaxLength="5" Width="50" Text='<%# Bind("Qty2") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<span id="Empty">No Details For This Such and Such.</span>
<img src="Images/icons/table_add.png" style="border:0px" alt="Add New Part" />
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="Gainsboro" ForeColor="#284775" />
</asp:GridView>
And then the code behind... this is very rough...
private void ApplyChanges()
{
foreach (GridViewRow row in this.gvDetails.Rows)
{
//do something with cells and data objects
//and then save add to list, save, etc.
}
}
EDIT
Above is where you will get your editing of the data. You can work with DataTables, DataViews, DataSets, and as the other solution shows, you can bind your grid to a list of your object. This should be determined based upon the data object model in your system and how the rows would be generated.