AutoGenerateInsertButton in Gridview - c#

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.

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.

Microsoft.AspNet.EntityDataSource gives wrong entity's property value during Updating or Deleting

I am using the Microsoft.AspNet.EntityDataSource like this:
ASPX:
<asp:GridView runat="server" DataSourceID="eds" ID="gv" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID" HeaderText="#" ReadOnly="true" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="true" />
<asp:CommandField ButtonType="Button" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<ef:EntityDataSource runat="server" ID="eds" ConnectionString="name=Entities"
EnableDelete="true" EnableInsert="True" EnableUpdate="True"
EntitySetName="awCategoryGroups" DefaultContainerName="Entities" EnableFlattening="False"
Where="it.isValid=true" OrderBy="it.Name asc" OnDeleting="eds_Deleting">
</ef:EntityDataSource>
It creates a table with records (entities) of type awCategoryGroup. The problem is, when I want to delete the record using the delete button from GridView CommandField whic calls the eds_Deleting method.
C#:
protected void eds_Deleting(object sender, Microsoft.AspNet.EntityDataSource.EntityDataSourceChangingEventArgs e)
{
awCategoryGroup cg = (awCategoryGroup)e.Entity;
cg.isValid = false;
e.Context.SaveChanges();
e.Cancel = true;
}
The database looks like this when selecting data for GridView.
DB:
ID Name Code isValid
==========================================
19 Roles UserRole True
20 Actions ActionType True
The goal should be to set isValid column to False. But when eds_Deleting method is called, the (awCategoryGroup)e.Entity contains corresponding values of its properties except the bool (in DB as bit) valued property isValid. It should be True, but it is False already before I set it.
So no save occures when calling e.Context.SaveChanges();, because the properties are not modified.
Is it a bug or am I doing something wrong? This philosophy worked fine on EF4, but after migrating to EF6, there is the problem.
It looks like the isValid must be as one of the visible columns in the GridView.
So eds_Deleting exactly do not work with the entity from database but only with its part used in GridView. Values not used in GridView are of default value (usually null or false).
So this is inter alia the difference between EF5 EntityDataSource and EF6 EntityDataSource. EF5 version worked with the fully loaded object, but EF6 do not.

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.

ASP.NET GridView using 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.

Allowing one column to be edited but not another

i have an asp.net c# application.
my gridview has a datasource that has 2 fields.
1 field cannot be edited by the user, but i need the other one to be editable!
is this possible to do?
Set the ReadOnly="true" property on all that you don't want editable.
Take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.readonly.aspx
A quick example from that page
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
autogenerateeditbutton="true"
allowpaging="true"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/>
<asp:boundfield datafield="Address" headertext="Address"/>
<asp:boundfield datafield="City" headertext="City"/>
<asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>
</columns>
</asp:gridview>
In this case CustomerID and Company name are read only and can't change. Address, City and PostalCode can be edited.
Just set the ReadOnly option to true on the columns you DON'T want people to edit. The columns that don't have this set or have ReadOnly set to false are editable by the user when in edit mode.
If you use a SqlDataSource then you should make sure to remove the columns to update from the UpdateCommand in the SqlDataSource if not you will have a problem when you update the fields
You should be able to set ReadOnly on DataGridViewCell.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.readonly.aspx
gridView.Rows[rowIndex][colName].ReadOnly = true;

Categories

Resources