I have a gridview, in the gridview task, I choose Edit Column-> Selected Fields->Command Field.select insert button to True. And the New link field appear, and I check the HTML Source,
look like necessary codes are there, but when I click on the "New" link field, no response at all, but update is ok for me, delete I have not tested yet.
Below are codes for the gridview and sqldatasource :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="agreement_no,id" Font-Size="9pt" DataSourceID="SqlDataSource1" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True"></asp:CommandField>
<asp:BoundField DataField="agreement_no" HeaderText="agreement_no" ReadOnly="True" SortExpression="agreement_no"></asp:BoundField>
<asp:TemplateField HeaderText="status" SortExpression="status">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="status" DataValueField="status" SelectedValue='<%# Bind("status") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" SelectCommand="SELECT DISTINCT [status] FROM [deal_master]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sales_person" HeaderText="sales_person" SortExpression="sales_person"></asp:BoundField>
<asp:BoundField DataField="swap_carrier" HeaderText="swap_carrier" SortExpression="swap_carrier"></asp:BoundField>
<asp:BoundField DataField="start_pos" HeaderText="start_pos" SortExpression="start_pos"></asp:BoundField>
<asp:BoundField DataField="end_pos" HeaderText="end_pos" SortExpression="end_pos"></asp:BoundField>
<asp:BoundField DataField="swap_commitment" HeaderText="swap_commitment" SortExpression="swap_commitment"></asp:BoundField>
<asp:BoundField DataField="zone" HeaderText="zone" SortExpression="zone"></asp:BoundField>
<asp:BoundField DataField="target_minutes" HeaderText="target_minutes" SortExpression="target_minutes"></asp:BoundField>
<asp:BoundField DataField="target_sell_rate" HeaderText="target_sell_rate" SortExpression="target_sell_rate"></asp:BoundField>
<asp:BoundField DataField="target_buy_rate" HeaderText="target_buy_rate" SortExpression="target_buy_rate"></asp:BoundField>
<asp:BoundField ConvertEmptyStringToNull="false" DataField="supplier_interconnect" HeaderText="supplier_interconnect" SortExpression="supplier_interconnect"></asp:BoundField>
<asp:BoundField ConvertEmptyStringToNull="false" DataField="customer_interconnect" HeaderText="customer_interconnect" SortExpression="customer_interconnect"></asp:BoundField>
<asp:BoundField DataField="target_sales" HeaderText="target_sales" SortExpression="target_sales"></asp:BoundField>
<asp:BoundField DataField="target_cost" HeaderText="target_cost" SortExpression="target_cost"></asp:BoundField>
<asp:BoundField DataField="target_profit" HeaderText="target_profit" SortExpression="target_profit"></asp:BoundField>
<asp:BoundField DataField="lcr_zone" HeaderText="lcr_zone" SortExpression="lcr_zone"></asp:BoundField>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" ReadOnly="True"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" DeleteCommand="DELETE FROM [deal_master] WHERE [agreement_no] = #agreement_no AND [id] = #id" InsertCommand="INSERT INTO [deal_master] ([agreement_no], [status], [sales_person], [swap_carrier], [start_pos], [end_pos], [swap_commitment], [zone], [target_minutes], [target_sell_rate], [target_buy_rate], [supplier_interconnect], [customer_interconnect], [target_sales], [target_cost], [target_profit], [lcr_zone], [id]) VALUES (#agreement_no, #status, #sales_person, #swap_carrier, #start_pos, #end_pos, #swap_commitment, #zone, #target_minutes, #target_sell_rate, #target_buy_rate, #supplier_interconnect, #customer_interconnect, #target_sales, #target_cost, #target_profit, #lcr_zone, #id)" SelectCommand="SELECT [agreement_no], [status], [sales_person], [swap_carrier], [start_pos], [end_pos], [swap_commitment], [zone], [target_minutes], [target_sell_rate], [target_buy_rate], [supplier_interconnect], [customer_interconnect], [target_sales], [target_cost], [target_profit], [lcr_zone], [id] FROM [deal_master]" UpdateCommand="UPDATE [deal_master] SET [status] = #status, [sales_person] = #sales_person, [swap_carrier] = #swap_carrier, [start_pos] = #start_pos, [end_pos] = #end_pos, [swap_commitment] = #swap_commitment, [zone] = #zone, [target_minutes] = #target_minutes, [target_sell_rate] = #target_sell_rate, [target_buy_rate] = #target_buy_rate, [supplier_interconnect] = #supplier_interconnect, [customer_interconnect] = #customer_interconnect, [target_sales] = #target_sales, [target_cost] = #target_cost, [target_profit] = #target_profit, [lcr_zone] = #lcr_zone WHERE [agreement_no] = #agreement_no AND [id] = #id">
<DeleteParameters>
<asp:Parameter Name="agreement_no" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="agreement_no" Type="String" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="sales_person" Type="String" />
<asp:Parameter Name="swap_carrier" Type="String" />
<asp:Parameter Name="start_pos" Type="Decimal" />
<asp:Parameter Name="end_pos" Type="Decimal" />
<asp:Parameter Name="swap_commitment" Type="String" />
<asp:Parameter Name="zone" Type="String" />
<asp:Parameter Name="target_minutes" Type="Decimal" />
<asp:Parameter Name="target_sell_rate" Type="Decimal" />
<asp:Parameter Name="target_buy_rate" Type="Decimal" />
<asp:Parameter Name="supplier_interconnect" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="false" Name="customer_interconnect" Type="String" />
<asp:Parameter Name="target_sales" Type="Decimal" />
<asp:Parameter Name="target_cost" Type="Decimal" />
<asp:Parameter Name="target_profit" Type="Decimal" />
<asp:Parameter Name="lcr_zone" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="sales_person" Type="String" />
<asp:Parameter Name="swap_carrier" Type="String" />
<asp:Parameter Name="start_pos" Type="Decimal" />
<asp:Parameter Name="end_pos" Type="Decimal" />
<asp:Parameter Name="swap_commitment" Type="String" />
<asp:Parameter Name="zone" Type="String" />
<asp:Parameter Name="target_minutes" Type="Decimal" />
<asp:Parameter Name="target_sell_rate" Type="Decimal" />
<asp:Parameter Name="target_buy_rate" Type="Decimal" />
<asp:Parameter ConvertEmptyStringToNull="false" Name="supplier_interconnect" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="false" Name="customer_interconnect" Type="String" />
<asp:Parameter Name="target_sales" Type="Decimal" />
<asp:Parameter Name="target_cost" Type="Decimal" />
<asp:Parameter Name="target_profit" Type="Decimal" />
<asp:Parameter Name="lcr_zone" Type="String" />
<asp:Parameter Name="agreement_no" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" SelectCommand="SELECT DISTINCT [agreement_no] FROM [deal_master] ORDER BY [agreement_no]"></asp:SqlDataSource>
<br />
<br />
<div>
</div>
enter image description here
my question is : what is the actions or setting I missed?
I want to know what steps I missed?
My objective is : I want to click on the "New" linkfield then 1 empty row appear, then I input the value in the new created row and I can save it or cancel it
If you look at the documentation for the GridView control, it says:
Inserting records into the data source is not directly supported by the GridView control. However, it is possible to insert records by using the GridView control in conjunction with the DetailsView or FormView control. For more information, see DetailsView or FormView, respectively.
Additionally, the CommandField documentation states about the ShowInsertButton property:
This property applies only to data-bound controls that support insert operations, such as the DetailsView control.
So, you'll need to create your own form separately, or leverage a DetailsView or FormView. Or better yet: ditch Web Forms altogether if you can, as it's a dead platform, as I explain on my blog.
im try to edit row in gridview called(gvTeam)
the gridview using objectdatasource(odsGvTeam)
in Admin_team.aspx
<asp:GridView ID="gvTeam" runat="server" AutoGenerateColumns="False" DataKeyNames="team_id" DataSourceID="odsGvTeam" AllowPaging="True">
<Columns>
<asp:BoundField DataField="team_id" HeaderText="team_id" InsertVisible="False" ReadOnly="True" SortExpression="team_id" Visible="False" />
<asp:BoundField DataField="team_name" HeaderText="key_team_name" SortExpression="team_name" />
<asp:BoundField DataField="team_lang" HeaderText="key_team_lang" SortExpression="team_lang" />
<asp:BoundField DataField="team_status" HeaderText="key_status" SortExpression="team_status" />
<asp:BoundField DataField="team_key" HeaderText="key_team_key" SortExpression="team_key" />
<asp:BoundField DataField="team_rating" HeaderText="key_team_rating" SortExpression="team_rating" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkEditTeam" runat="server" CausesValidation="false" CommandName="edit_team" Text="Edit" CommandArgument='<%# Bind("team_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDeleteTeam" runat="server" CausesValidation="false" OnClientClick="return confirm('Are you sure?');" CommandName="delete_team" Text="Delete" CommandArgument='<%# Bind("team_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsGvTeam" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="CMS_NG_Sql_V1.dsTeamTableAdapters.taTeam" UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="original_team_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="team_name" Type="String" />
<asp:Parameter Name="team_lang" Type="String" />
<asp:Parameter Name="team_status" Type="Int32" />
<asp:Parameter Name="team_key" Type="String" />
<asp:Parameter Name="team_rating" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:Parameter DefaultValue="en" Name="team_lang" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="team_name" Type="String" />
<asp:Parameter Name="team_lang" Type="String" />
<asp:Parameter Name="team_status" Type="Int32" />
<asp:Parameter Name="team_key" Type="String" />
<asp:Parameter Name="team_rating" Type="Int32" />
<asp:Parameter Name="team_id" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
CodeBehing
Protected Sub gvTeam_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvTeam.RowCommand
Select Case e.CommandName.ToLower
Case "edit_team"
txtID.Value = e.CommandArgument
Using objTeam As New cls_Team(CInt(txtID.Value))
txtID.Value = objTeam.TeamDataRow.team_id
txtLang.Text = objTeam.TeamDataRow.team_lang
txtRating.Text = objTeam.TeamDataRow.team_rating
txtTeamKey.Text = objTeam.TeamDataRow.team_key
optStatus.SelectedValue = objTeam.TeamDataRow.team_status
txtName.Text = objTeam.TeamDataRow.team_name
MultiView1.SetActiveView(viwEditTeam)
End Using
AdminLogFile(CMS_NG_Sql_V1.enum_AdminLogFile.Edit)
Case "delete_team"
Dim intID As Integer = e.CommandArgument
'Response.Redirect("admin_team.aspx?IDTeam=" & intID)
Using objTeam As New cls_Team(intID)
objTeam.Delete(intID)
AdminLogFile(CMS_NG_Sql_V1.enum_AdminLogFile.Delete)
gvTeam.DataBind()
End Using
End Select
End Sub
the Case "delete_team" work fine
but the Case "edit_team" not worked
i got error:
also i used dataset called (dsTeam.xsd)
how can solved IT ?
A value in the team_rating column is NULL. A database NULL different from a string variable in VB that may have a null value. You need to check for this before assigning a value to txtRating.Text:
If Not objTeam.TeamDataRow.IsNull("team_rating") Then
txtRating.Text = objTeam.TeamDataRow.team_rating
End If
So I've got a gridview bound to a SQL datasource stored procedure. The stored procedure returns a pivot table, where a number of columns are unknown until run time. I therefore need to autogenerate the columns.
The gridview is intended to allow Excel-like data updates. So the goal is to have the grid load as textboxes. I know if I were to declare the columns on my aspx page, I could create template columns and handle their visibility that way.
So I believe I need a way to programmatically set all the columns to template fields (without knowing the column names), or I need to discover the method by which I can just "flip the switch" and just make everything editable.
Thanks in advance!
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSourceExcelGridTest">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceExcelGridTest" runat="server" ConnectionString="<%$ ConnectionStrings:WebAppsConnectionString %>" SelectCommand="spJobForecastingGetEmployeesByDepartmentAndProject_v1" SelectCommandType="StoredProcedure" UpdateCommand="spJobForecastingUpdateHours" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="DepartmentNumber" Type="String" DefaultValue="13211" />
<asp:Parameter Name="ProjectNumber" Type="String" DefaultValue="13211" />
<asp:Parameter Name="startDate" Type="DateTime" DefaultValue="2/21/2014" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DepartmentNumber" Type="String" />
<asp:Parameter Name="ProjectNumber" Type="String" />
<asp:Parameter Name="Alias" Type="String" />
<asp:Parameter Name="WorkWeek" Type="DateTime" />
<asp:Parameter Name="WorkHours" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
try this:
UPDATED CODE
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSourceExcelGridTest">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceExcelGridTest" runat="server" ConnectionString="<%$ ConnectionStrings:WebAppsConnectionString %>" SelectCommand="spJobForecastingGetEmployeesByDepartmentAndProject_v1" SelectCommandType="StoredProcedure" UpdateCommand="spJobForecastingUpdateHours" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Textbox ID="DepartmentNumber" DefaultValue="13211" text='<%# Eval("DepartmentNumber") %>'></asp:TextBox>
<asp:Textbox ID="ProjectNumber" Type="String" DefaultValue="13211" Text='<%# Eval("ProjectNumber") %>'></asp:TextBox>
<asp:Textbox ID="startDate" Type="DateTime" DefaultValue="2/21/2014" Text='<%# Eval("startDate") %>'></asp:TextBox>
</SelectParameters>
<UpdateParameters>
<asp:Textbox ID="DepartmentNumber" Type="String" text='<%# Eval("DepartmentNumber") %>'></asp:TextBox>
<asp:Textbox ID="ProjectNumber" Type="String" text='<%# Eval("ProjectNumber") %>'></asp:TextBox>
<asp:Textbox ID="Alias" Type="String" text='<%# Eval("Alias") %>'></asp:TextBox>
<asp:Textbox ID="WorkWeek" Type="DateTime" text='<%# Eval("WorkWeek") %>'></asp:TextBox>
<asp:Textbox ID="WorkHours" Type="Int32" text='<%# Eval("WorkHours") %>'></asp:TextBox>
</UpdateParameters>
</asp:SqlDataSource>
When working with ObjectDataSource I have following select:
<asp:DropDownList runat="server" SelectedValue='<%# Bind("InceptionCycle.ID") %>' ID="InceptionCycle"
DataSourceID="odsAllCycles" DataTextField="CycleName" DataValueField="ID" AppendDataBoundItems="true">
<asp:ListItem Text="Choose..." Value=""></asp:ListItem>
</asp:DropDownList>
and ObjectDataSource configured with UpdateMethod and InsertMethod. However insert/update methods have parameters with InceptionCycle name (because InceptionCycle.ID is not valid identifier).
Is there a way to instruct ObjectDataSource to take InceptionCycle.ID form parameter and place in as InceptionCycle method parameter?
Here's full ObjectDataSource code:
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Sources.DomainSource"
SelectMethod="FindById" InsertMethod="Add" UpdateMethod="Update"
OnInserted="ods_Inserted" OnUpdated="ods_Updated">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
</SelectParameters>
<InsertParameters>
<asp:Parameter ConvertEmptyStringToNull="true" Name="ShortTitle" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="WMRId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="MDSId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="Status" />
</InsertParameters>
<UpdateParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="ShortTitle" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="WMRId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="MDSId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="Status" />
</UpdateParameters>
</asp:ObjectDataSource>
You can only use the Bind syntax with a top level property - it doesn't support nested properties (VS 2012 will, though).
So, you should create a new property on your data source like below and bind the SelectedValue to this new property instead
public string InceptionCycleID
{
get
{
return InceptionCycle.ID;
}
set
{
InceptionCycle.ID = value;
}
}
I have an ObjectDataSource that I'm using with a FormView, and it works fine, but i want to change one small thing. On the FormView the button that fires the update has the CommandName attribute set to "Update," but I would like to change that attribute to something other than "Update" - when I do change that attribute the update no longer works. The reason I want to do this is I have multiple FormViews on the same page and need to have multiple update buttons. Below is my code:
FormView:
<asp:FormView ID="fvGeneralInfo" runat="server"
DataSourceID="objInstructorDetails" CssClass="Gridview"
OnItemCommand="fvGeneralInfo_ItemCommand"
DefaultMode="Edit">
<EditItemTemplate>
<table>
....
<tr>
<td style="text-align:right;">
<asp:Label runat="server" ID="lblGeneralInfoMessage" Text="General Info updated successfully" Visible="false" />
</td>
<td>
<asp:Button runat="server" ID="btnUpdateGeneralInfo" ValidationGroup="UpdateGeneralInfo" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnCancelGeneralInfo" Text="Cancel" CommandName="CancelGeneralInfo" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
ObjectDataSource:
<asp:ObjectDataSource ID="objInstructorDetails" runat="server" TypeName="AIMLibrary.BLL.Instructor" SelectMethod="GetInstructorDetails"
InsertMethod="InsertInstructor" UpdateMethod="UpdateInstructor" OnInserting="objInstructorDetails_OnInserting"
OnUpdating="objInstructorDetails_OnUpdating" >
<SelectParameters>
<asp:QueryStringParameter Name="InstructorId" QueryStringField="InstructorId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="instructorId" Type="Int32" />
<asp:Parameter Name="firstName" Type="String" DefaultValue="" />
<asp:Parameter Name="middleName" Type="String" DefaultValue="" />
<asp:Parameter Name="lastName" Type="String" DefaultValue="" />
<asp:Parameter Name="phone" Type="String" DefaultValue="" />
<asp:Parameter Name="email" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine1" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine2" Type="String" DefaultValue="" />
<asp:Parameter Name="city" Type="String" DefaultValue="" />
<asp:Parameter Name="state" Type="String" DefaultValue="" />
<asp:Parameter Name="zip" Type="String" DefaultValue="" />
<asp:Parameter Name="abcBoardNumber" Type="String" DefaultValue="" />
</UpdateParameters>
</asp:ObjectDataSource>
Each FormView will have it's own event for handling updates so the CommandName being the same for different FormViews should not be a problem.
You can change the name of the buttons if that's an issue by changing it's text value.