Stored Procedure some columns to gridview - c#

I would like to apologize if this has been asked in a different way before. I couldn't find any links specific to this question.
I'm currently learning how to implement GridViews on an ASP.Net web forms application, changing to grid views from a repeater. The repeaters had been binding to some columns retrieved by a stored procedure on our SQL Server and then displaying to the page. Due to the large amount of data, we decided that using the paging feature associated with grid views would improve our performance. However, when I have the grid view bind to the data source, all of the columns from the stored procedure are displayed. I believe I have the syntax correct:
<asp:GridView ID="CECreditGridView" runat="server" DataSourceID="testSqlSource" AllowPaging="true" PageSize="100">
<Columns>
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkView" />
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Update.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkEdit" />
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkDelete" />
<asp:BoundField HeaderText="ID" DataField="CECredit_ID"/>
<asp:HyperLinkField HeaderText="Session Title" DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" DataTextField="CECredit_Name" />
<asp:HyperLinkField HeaderText="Person" DataNavigateUrlFormatString="Person_Update.aspx?Person_ID={0}" DataNavigateUrlFields="Person_ID" DataTextField="FullName" />
<asp:HyperLinkField HeaderText="Type" DataNavigateUrlFormatString=“CECreditType_Update.aspx?CECreditType_ID={0}" DataNavigateUrlFields="CECreditType_ID" DataTextField="CECreditType_Name" />
<asp:HyperLinkField HeaderText="Status" DataNavigateUrlFormatString="CECreditStatus_Update.aspx?CECreditStatus_ID={0}" DataNavigateUrlFields="CECreditStatus_ID" DataTextField="CECreditStatus_Name" />
<asp:BoundField HeaderText="Expiration Date" DataFormatString = "{0:MM/dd/yyyy}" DataField="CECredit_ExpirationDate" />
<asp:BoundField HeaderText="Last Updated" DataFormatString = "{0:MM/dd/yyyy}" DataField="CECredit_RecordUpdateDate" />
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />
</asp:GridView>
So my question is, is there a way to only have the called columns display? Or will I need to change the stored procedure to only return the columns I want displayed? We were trying to have the stored procedure stay the same because the code behind used a few of the other columns for validation, etc.

Related

Relationship one to one in GridView in asp.net C# application

I have the following GridView in my project:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="329px">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="imie" HeaderText="imie" ReadOnly="True" SortExpression="imie" />
<asp:BoundField DataField="nazwisko" HeaderText="nazwisko" ReadOnly="True" SortExpression="nazwisko" />
<asp:BoundField DataField="idz" HeaderText="idz" ReadOnly="True" SortExpression="idz" />
<asp:BoundField DataField="idp" HeaderText="idp" ReadOnly="True" SortExpression="idp" />
<asp:BoundField DataField="ids" HeaderText="ids" ReadOnly="True" SortExpression="ids" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="ProjektZaliczenieKoncowy.DBClassesDataContext" EntityTypeName="" OrderBy="id" Select="new (id, imie, nazwisko, idz, idp, ids)" TableName="pracownicies">
</asp:LinqDataSource>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
In field "idz" I want to show the date from the column "Zawod" in the table "Zawody" where idz = id. It should be a one to one relationship.
Here's my database diagram:
Diagram
It's probably easy but I can't find any solutions.
You should make a stored procedure in your database which gets you all the columns you need with the one "Zawod" in the table "Zawody" where idz = id. After that you call this stored procedure with c#.
In your code you should have object which represents the entities from the database. You get the information from the database parse it to an object and after that you bind your list of these object to your gridview and there you can get the column you want from the other table.
If you want to do it without a stored procedure you can just write manually the text of the query and pass it as SQLCommand the continuation is the same anyway so the stored procedure is preferable.

Copy gridview cell value to the clipboard

I have a gridview with some values. If the user clicks on a link or button (here I took a Buttonfield with the text 'Copy') on one of the rows, it should copy a cell value of that row to the clipboard.
Is something like this possible in asp.net c# ?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" Visible="False" OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Password" HeaderText="Password"
SortExpression="Password" />
<asp:CommandField ButtonType="Image" EditImageUrl="/images/edit.png" CancelImageUrl="/images/undo.png" UpdateImageUrl="/images/save.png" ShowEditButton="True" />
<asp:CommandField ButtonType="Image" ShowDeleteButton="True" DeleteImageUrl="/images/deletered.png" />
<asp:ButtonField Text="Copy" />
</Columns>
<EmptyDataTemplate>
No data available
</EmptyDataTemplate>
</asp:GridView>
There is no Onclick event available for a ButtonField. Otherwise I would think of using some javascript (I have seen that in other solutions)
Thanks for any input.
You can do that with windows form only, but since you are working on Web application you should write client side code which is javascript to copy data on client side not server side. checkout this link

adding GridView DataKeyNames values gives Resources.ExceptionPolicyNotFound

I'm adding a tooltip to a GridView, and am running across an interesting error. Everything works fine when I have the bound field I'm using to populate my tooltip visible. When it is hidden, the tooltips are blank (which I expected, since the data doesn't get sent when hidden). To get around this, I'm using the DataKeyNames attribute to keep the data. When this is added, it results in the Resources.ExceptionPolicyNotFound exception.
Here is the grid view (with reject reason):
<asp:GridView ID="GrdSearchResult" runat="server" AutoGenerateColumns="False" OnRowDataBound="GrdSearchResult_RowDataBound"
Width="100%" AllowPaging="True" OnPageIndexChanging="GrdSearchResult_PageIndexChanging"
EmptyDataText="No record found." PageSize="20" CssClass="GridStyle" PagerStyle-CssClass="PagerStyle" EmptyDataRowStyle-CssClass="EmptyStyle"
DataKeyNames="RejectReason">
<Columns>
<asp:BoundField HeaderText="Reject Code" DataField="RejectCode"/>
<asp:BoundField HeaderText="Claim #" DataField="ClaimNbr" />
<asp:BoundField HeaderText="Member Medicaid #" DataField="MemberMedicaidNbr" />
<asp:BoundField HeaderText="Member Name" DataField="MemberName" />
<asp:BoundField HeaderText="Provider Name" DataField="ProviderName" />
<asp:BoundField HeaderText="Check(s) Details" />
<asp:BoundField HeaderText="Date of Service" DataFormatString="{0:MM-dd-yyyy}" DataField="ServiceRange" />
<asp:BoundField HeaderText="Claims Link" />
<asp:BoundField HeaderText="RejectReason" DataField="RejectReason" Visible="false" />
</Columns>
</asp:GridView>
This is the code that uses the column, but it never gets hit prior to the error.
e.Row.Cells[0].ToolTip = e.Row.Cells[8].Text.ToString();
The goal is to set the first column's tooltip to the value of the last (9th) column.
I am still not sure what caused the error, but I've decided to handle this in a different way. Instead of hiding the data field by default, I'm hiding it after I set the tooltip.
e.Row.Cells[0].ToolTip = e.Row.Cells[8].Text.ToString();
e.Row.Cells[8].Visible = false;
The only wrinkle was getting the column header hidden as well, but I managed that with identical code in another section. The existing code uses
if (e.Row.Cells[0].Text != string.Empty)
To look at only data rows and not the header row, so I added.
else
{
e.Row.Cells[8].Visible = false;
}
After the data manipulation was done.

User Control Properties?

Can I build a control that basically acts "like a MasterPage"?
What I mean to do is, say I have a grid like this in a number of pages:
<asp:UpdatePanel ID="AnnouncementsPanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="AnnoucementsGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" >
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Created" HeaderText="Date" />
<asp:BoundField DataField="Modified" HeaderText="Last Modified" />
<asp:ButtonField ButtonType="Button" Text="Process" CommandName="Process" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
I'd like to build a control that handles most of the codebehind, but I need to declare the columns for the grid declaratively in each case.
Is there a way to create a control like this?
<uc:CrudGrid ID="AnnouncementsCrud" runat="server">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Created" HeaderText="Date" />
<asp:BoundField DataField="Modified" HeaderText="Last Modified" />
<asp:ButtonField ButtonType="Button" Text="Process" CommandName="Process" />
</Columns>
</uc:CrudGrid>
or event better:
<uc:CrudGrid ID="AnnouncementsCrud" runat="server">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Created" HeaderText="Date" />
<asp:BoundField DataField="Modified" HeaderText="Last Modified" />
</Columns>
</uc:CrudGrid>
Maybe having to name the tag "Fields", but being able to drop the button, so it can be used in the UC's code-behind?
A lot of love to whoever has a positive answer on this <3
Found the exact solution I was looking for:
[DefaultValue((string)null)]
[Editor(typeof(System.Web.UI.Design.WebControls.DataControlFieldTypeEditor), typeof(UITypeEditor))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public DataControlFieldCollection Columns
{
get { return Grid.Columns; }
}
this way I can expose the Columns from the grid in my own user control and edit the fields from the markup, keeping general functionalities within my UC
Depends on how positive you want it to be :-) You can always build your own control to do this. That is one option.
Another option is to create a helper object that attaches to the grid, and keep each grid separate.
Third option is to create a user control with the common code, and programmably add columns to the grid through the user control.
HTH.

How do I insert Data Using the EntityDataSource control?

On my page there are textboxes which I use for input. How can I wire those textboxes to an EntityDataSource control?
Instead of using plain text boxes with your EntityDataSource control, I would recommend using a GridView control with a corresponding DetailsView control. The EntityDataSource control was designed to work nicely with these two controls. The DetailsView control will be the one which is actually responsible for the inserting and updating of your data.
When you are configuring the EntityDataSource controls (you will need two, one for the GridView and one for the DetailsView), make sure to check the 'Enable automatic inserts,' 'Enable automatic updates,' and 'Enable automatic deletes' check boxes in the 'Configure Data Selection' screen of the setup wizard. In your case, since you are only interested in inserting the data, you can just check the 'Enable automatic inserts' checkbox.
You can also just use the DetailsView by itself, and leave the GridView out. To do this, you'll need to configure the DetailsView's EntityDataSoruce 'Expression Editor' for the 'Where' clause a bit differently (found in the properties list of the EntityDataSource control).
It's important to point out that the GridView and DetailsView are pretty heavy controls. Unless you are building a small site (with few users) or prototyping a planned site project, I would recommend revising your development strategy and possibly going with raw ADO.Net instead.
Here is a simple example:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=SomeDatabaseEntities"
DefaultContainerName="SomeDatabaseEntities"
EnableFlattening="False"
EntitySetName="Links" EnableDelete="True" EnableInsert="True"
EnableUpdate="True">
</asp:EntityDataSource>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
ForeColor="#333333"
GridLines="None" AllowPaging="True" AllowSorting="True"
EnableViewState="false"
DataSourceID="EntityDataSource1"
AutoGenerateColumns="False" PageSize="20"
DataKeyNames="LinkId">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="LinkId" HeaderText="LinkId"
SortExpression="LinkId" ReadOnly="True" />
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="URL" HeaderText="URL"
SortExpression="URL" />
<asp:BoundField DataField="ImagePath" HeaderText="ImagePath"
SortExpression="ImagePath" />
</Columns>
</asp:GridView>
<br />
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ConnectionString="name=SomeDatabaseEntities"
DefaultContainerName="SomeDatabaseEntities"
EnableFlattening="False"
EntitySetName="Links" Where="it.LinkId == #LinkId"
EnableInsert="True"
EnableUpdate="True">
<WhereParameters>
<asp:ControlParameter ControlID="GridView1"
DbType="Int32" Name="LinkId"
PropertyName="SelectedValue" />
</WhereParameters>
</asp:EntityDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px"
Width="125px"
CellPadding="4" ForeColor="#333333"
GridLines="None" DataKeyNames="LinkId"
AutoGenerateRows="False"
DataSourceID="EntityDataSource2">
<Fields>
<asp:BoundField DataField="LinkId"
HeaderText="LinkId"
ReadOnly="true" InsertVisible="false"
SortExpression="LinkId" />
<asp:BoundField DataField="Title"
HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="URL" HeaderText="URL"
SortExpression="URL" />
<asp:BoundField DataField="ImagePath"
HeaderText="ImagePath"
SortExpression="ImagePath" />
<asp:CommandField ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>

Categories

Resources