ASP.NET GridView using C# - c#

I have a problem with gridview, I am using a gridview and listview on a page, when I select a row in the gridview I want to insert the data into the listview based on selection in the gridview. I am using a primary key in gridview.
PLease help me out

This is also called "Master-Detail".
You can find a great tutorial here.
In short, you need to make the row selectable.
<asp:GridView ID="ProductsGrid" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="AllProductsDataSource" EnableViewState="False">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ProductName"
HeaderText="Product" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:c}" HeaderText="Unit Price"
HtmlEncode="False" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
This is done with <asp:CommandField ShowSelectButton="True" /> in the code above.
The next step is to add you DetailsView to be binded to the selected element of the master gridview. This can be done because the selection return the primary key of the element, from there you can load the detail.
This do a PostBack, if you want you can use Ajax and remove the PostBack flickering.

I believe the select on the gridview should fire off the event SelectedIndexChanged(). You would use that event to create the datasource for your listview and then bind the datasource to your listview.

Related

Stored Procedure some columns to gridview

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.

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.

GridView Checkbox - Cannot Change Properties

I have an ASP GridView with a column that contains CheckBox controls. These CheckBoxes are bound to a bit field accessed via a SqlDataSource. The GridView is editable and the CheckBoxes are disabled on load.
I am trying to get the enabled property to change to true during edit, so the CheckBox in the row that is being edited can be changed, and this change updated to the bit field in the database.
The ASP code
<asp:UpdatePanel ID="upGridView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvChecklist" runat="server"
AutoGenerateColumns="false" DataSourceID="dsChecklist"
AutoGenerateEditButton="true" onrowupdating="gvChecklist_RowUpdating"
onrowediting="gvChecklist_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Finished">
<ItemTemplate><asp:CheckBox ID="cbFinished" runat="server" Enabled="false" Checked='<%# Eval("Finished") ?? false %>' /> </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Division"
HeaderText="Division"
readonly="true" />
<asp:BoundField DataField="Application"
HeaderText="Application"
readonly="true" />
<asp:BoundField DataField="Task"
HeaderText="Task"
readonly="true" />
<asp:BoundField DataField="TestedBy" HeaderText="Tested By" readonly="true"/>
<asp:BoundField DataField="Notes" HeaderText="Notes" ReadOnly="false"/>
<asp:BoundField DataField="JiraTicket"
HeaderText="JIRA Ticket"
readonly="false" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C# Code Behind for the RowEditing event
protected void gvChecklist_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gvChecklist = (GridView)LoginView1.FindControl("gvChecklist");
CheckBox cb = (CheckBox)gvChecklist.Rows[e.NewEditIndex].Cells[1].FindControl("cbFinished");
cb.Enabled = true;
gvChecklist.DataBind();
}
-edit: I felt the need to add a little more information here. The code to set the CheckBox cb to the GridView Row's CheckBox seems to be working correctly, as I can see the enabled property = true through the debugger. I am wondering if it is getting set back to false due to PostBack after this event runs. The Load method for this page is empty, but I am thinking that the enabled=false code is being hit in the ASP page.
UI of GridView
The solution for this was surprisingly simple. Instead of putting a CheckBox in a TemplateField, I was able to set a DataField property for the CheckBox column. This allowed the edit functionality to work with no further C# code.
<asp:CheckBoxField DataField="Finished"
HeaderText="Finished"
readonly="false" />

AutoGenerateInsertButton in Gridview

I have the following GridView
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnRowCommand="GridView1_RowCommand" DataKeyNames="Chart_Id"
AutoGenerateColumns="False" EnableModelValidation="True" >
<Columns>
<asp:CommandField ShowEditButton="False" ShowDeleteButton="False" ShowInsertButton="False" />
<asp:BoundField DataField="Week" HeaderText="Week" SortExpression="Week" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ItemStyle-Wrap="False" />
<asp:BoundField DataField="Host" HeaderText="Host" SortExpression="Host" />
<asp:BoundField DataField="Topic_1" HeaderText="Topic 1" SortExpression="Topic_1" />
<asp:BoundField DataField="Topic_2" HeaderText="Topic 2" SortExpression="Topic_2"
HeaderStyle-Wrap="False" />
<asp:BoundField DataField="Topic_3" HeaderText="Topic 3" SortExpression="Topic_3" />
<asp:BoundField DataField="Topic_4" HeaderText="Topic 4" SortExpression="Topic_4" />
</Columns>
</asp:GridView>
By default, I have the edit/insert/cancel buttons set to false.
Then in the code behind, I want to be able to set these to true during certain conditions.
string theUser = Helpers.GetUser();
string admin = "adminName";
if (theUser == admin) {
// Set the buttons to true
}
I've been looking for ways to do this, and someone suggested to use the AutoGenerate properties, and then enable them like so:
GridView1.AutoGenerateEditButton = true;
GridView1.AutoGenerateDeleteButton = true;
GridView1.AutoGenerateInsertButton = true; // This one throws an error
Only problem is, AutogenerateInsertButton does not seem to exist, in the main ASPX page or in the code behind.
Can anyone suggest some ways for me to access these properties and set them to true?
Thank you.
Why do you think that a GridView should have an AutoGenerateInsertButton property?
A GridView is a list of GridViewRows, where each row represents a record/element/item which can be edited or deleted. But it doesn't make sense to have a insert-button for each record because it already exists.
You could follow this tutorial which shows how to use the footer-row of the GridView to insert a new record.
The property AutoGenerateInsertButton exists on the DetailsView control. Whoever designed the control probably figured that you don't need an insert button for each row in the grid, since each would essentially do the same thing.
So, maybe you could display an empty DetailsView at the bottom of the grid, or just create your own insert command using a regular Button.

Categories

Resources