Bind Gridview Datasource with Linq (asp.net) - asp.net

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") %>'

Related

Label text databind to a column alias with dot

I have this Select statement:
SELECT recordID As [Zap.st.] FROM SomeTable
When I try to bind the result to a GridView with template fields using an asp:Label I get this error:
System.Web.HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Zap'
This is a sample GridView:
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("Zap.st.") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried:
Text='<%# Eval("[Zap.st.]") %>'
Text='<%# Eval("'Zap.st.'") %>'
but none of these seem to work.
Edit: To further clarify my problem:
My Select statement is a View on Sql Server, the results in my asp.net come in as [Zap.st.], there is no reference that this is [recordID]
Try this code :
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("recordID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I found a similar question on SO, this is the solution:
<asp:Label ID="lblZapStD" runat="server" Text='<%# DataBinder.GetPropertyValue(Container.DataItem, "Zap.st.") %>' />
And this is the original question with an accepted answer.

How can I pass parameters to a Crystal Report from my GridView?

I have an existing SQL database and a ASP.NET application. My application has two existing GridViews and login functions. I also have an existing Crystal Report designed to automatically create a receipt from my SQL database. This is done by the user filling out 3 specific parameters, and the rest of the data (which are in parallel with those parameters) will automatically fill out the crystal report.
I want to create a print button in my GridView to automatically fill out the 3 parameters in the Crystal Report. This is an attempt to make my application more user friendly. In short, the user would push the print button in its new column in the GridView, and the 3 parameters would be automatically picked up and filled into the Crystal Report.
My parameters are: "EmpID", "KeyControl", and "ControlNumber". My crystal report label is "x.rpt"
Here is my GridView markup:
<asp:GridView ID="gridKeyAndBuildingInformation" runat="server" CssClass="style3"
AllowSorting ="True"
AutoGenerateColumns ="False"
AllowPaging="True"
DataKeyNames="KeyRefId"
OnRowCancelingEdit="gridKeyAndBuildingInformation_RowCancelingEdit"
onPageIndexChanging="gridKeyAndBuildingInformation_PageIndexChanging"
OnRowDataBound="gridKeyAndBuildingInformation_RowDataBound"
OnRowEditing="gridKeyAndBuildingInformation_RowEditing"
OnRowUpdating="gridKeyAndBuildingInformation_RowUpdating"
OnRowCommand="gridKeyAndBuildingInformation_RowCommand"
ShowFooter="True"
OnRowDeleting="gridKeyAndBuildingInformation_RowDeleting"
AlternatingRowStyle-BackColor="#EFEFEF"
EditRowStyle-VerticalAlign="Top"
HeaderStyle-BackColor="#77b218"
OnSorting="gridKeyAndBuildingInformation_Sorting"
BackColor="#CCCCCC"
BorderColor="#999999"
BorderStyle="Solid"
BorderWidth="3px"
CellPadding="4"
EnableModelValidation="True"
ForeColor="Black"
CellSpacing="2">
<Columns>
<asp:TemplateField HeaderText ="EmpID" HeaderStyle-CssClass="HeaderText" sortexpression="EmpID">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpID" runat="server" Text='<%# Eval("EmpID") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewEmpID" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="ControlNumber" HeaderStyle-CssClass="HeaderText" sortexpression="ControlNumber">
<ItemTemplate>
<asp:Label ID="lblControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewControlNumber" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="KeyNumber" HeaderStyle-CssClass="HeaderText" sortexpression="KeyNumber">
<ItemTemplate>
<asp:Label ID="lblKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewKeyNumber" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:ButtonField HeaderText="Print" ShowHeader="True" Text="Print" />
</Columns>
<EditRowStyle VerticalAlign="Top"></EditRowStyle>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
You could handle the RowCommand event, which would fire when you click that delete button in your ButtonField column. Add this to the end of your GridView declaration markup (after "CellSpacing="2"" but before the ">"):
OnRowCommand="gridKeyAndBuildingInformation_RowCommand"
And then, in your code behind you would need something like this (this is C#, if you need VB.NET let me know - this question isn't tagged with a server-side language):
protected void gridKeyAndBuildingInformation_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Get your ID for the row you're on
int ID = Convert.ToInt32(e.CommandArgument);
// Get the row the button was clicked in
GridViewRow row = gridKeyAndBuildingInformation.Rows[ID];
// Get the values you need from that row
int EmpID = row.Cells[0];
int ControlNumber = row.Cells[1];
int KeyNumber = row.Cells[2];
// Use those numbers to make your call to the Crystal Report
// I don't know what this part would look like.
}
You can read more about the RowCommand event on MSDN: GridView.RowCommand Event

ASP.NET GridView Display TextBox if Row is Empty

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.

Persisting Checkbox state while paging in SPGridView

Suppose I have created a spgridview with the following code.
<SharePoint:SPGridView ID="spgridList" Visible="false" runat="server" AutoGenerateColumns="false"
Width="100%" AllowPaging="true" AllowSorting="true" AllowFiltering="true" PageSize="5"
OnPageIndexChanging="spgridList_PageIndexChanging" HeaderStyle-BackColor="Red"
onrowdatabound="spgridList_RowDataBound" onrowcommand="spgridList_RowCommand">
<PagerStyle CssClass="ms-descriptiontext" />
<HeaderStyle BackColor="Blue" ForeColor="Black" Font-Bold="true" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox CssClass="chkbox" Checked="false" ID="chkField" AutoPostBack="true"
OnCheckedChanged="chkField_CheckedChanged" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</SharePoint:SPGridView>
To persist the checkbox states between pagebacks ,currently i am using session states.It has some draw backs.Now how can I persist the checked states,when the user manually checks all the checkboxes,how can i make a check on the header checkbox?Provide some links
How about emulating it like a select CheckBox but disabling the focus of the row?
Selection of the rows is maintained across pages.
http://www.devexpress.com/Support/Center/p/E1559.aspx

Simple databinding to gridview columns

I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).

Resources