i have a gridview which simply displays records based on a filter from a dropdownlist, when i change the dropdownlist the page postbacks fine and the filter works, but when i click to edit a row the sort order changes based on the new filter. when i change the drop down back to the original filter and click edit the sort order remains the same.
any ideas?
<asp:DropDownList ID="ddlFilterDocs" runat="server"
AutoPostBack="True"
onselectedindexchanged="ddlFilterDocs_SelectedIndexChanged">
<asp:ListItem Text="MissingData"
Value="MissingData" >MissingData</asp:ListItem>
<asp:ListItem Text="AllData" Value="AllData"
>AllData</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" PageSize="35"
AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None"
onrowcommand="GridView1_RowCommand" DataKeyNames="customerCode"
onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField SortExpression="CustomerCode">
<ItemTemplate>
<asp:LinkButton ID="lbEdit" ForeColor="DarkGreen" Font-
Bold="True" CommandArgument='<%# Eval("customerCode") %>' CommandName="EditRow"
runat="server">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbUpdate" CommandArgument='<%#
Eval("customerCode") %>' CommandName="UpdateRow" runat="server"
ForeColor="White">Update</asp:LinkButton>
<asp:LinkButton ID="lblCancel" CommandArgument='<%#
Eval("customerCode") %>' CommandName="CancelUpdate" runat="server"
ForeColor="White">Cancel</asp:LinkButton>
</EditItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer ID"
SortExpression="CustomerCode">
<EditItemTemplate>
<asp:Label ID="lblCustCodeEdit" runat="server" Text='<%#
Eval("CustomerCode") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCustCode" runat="server" Text='<%#
Bind("CustomerCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name"
ReadOnly="True" >
<HeaderStyle Width="100px" />
</asp:BoundField>
<asp:TemplateField HeaderText="StreetAddress"
SortExpression="StreetAddress">
<EditItemTemplate>
<asp:TextBox ID="tbxStreetAddress" runat="server"
Text='<%# Bind("StreetAddress") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStreetAddress" runat="server" Text='<%#
Bind("StreetAddress") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:TextBox ID="tbxCity" runat="server" Text='<%#
Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%#
Bind("City") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip" SortExpression="Zip">
<EditItemTemplate>
<asp:TextBox ID="tbxZip" runat="server" Text='<%#
Bind("Zip") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblZip" runat="server" Text='<%#
Bind("Zip") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="50px" />
</asp:TemplateField>
<asp:BoundField DataField="DocNumber" HeaderText="DocNumber"
SortExpression="DocNumber" ReadOnly="True">
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="GrossAmount" HeaderText="Gross"
SortExpression="GrossAmount" ReadOnly="True"
DataFormatString="{0:c2}" >
<HeaderStyle Width="70px" />
</asp:BoundField>
<asp:BoundField DataField="NetAmount" HeaderText="Net"
SortExpression="NetAmount" ReadOnly="True" DataFormatString="
{0:c2}" >
<HeaderStyle Width="70px" />
</asp:BoundField>
<asp:BoundField DataField="VATAmount" HeaderText="VAT"
SortExpression="VATAmount" ReadOnly="True" DataFormatString="
{0:c2}" >
<HeaderStyle Width="70px" />
</asp:BoundField>
<asp:BoundField DataField="FromVoucNbr" HeaderText="Voucher"
SortExpression="FromVoucNbr" ReadOnly="True" >
<HeaderStyle Width="70px" />
</asp:BoundField>
<asp:BoundField DataField="LastModDate" HeaderText="Date"
SortExpression="LastModDate" ReadOnly="True"
DataFormatString="{0:G}" >
<HeaderStyle Width="70px" />
</asp:BoundField>
<asp:BoundField DataField="LastModUser" HeaderText="LastModUser"
SortExpression="LastModUser" ReadOnly="True">
<HeaderStyle Width="50px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
If you are rebinding the data into the Row_Editing event then you need to filter that with the drop down selected value.
Regards,
Related
hi i have a Gridview that load from the database and i create a column in the Gridview name lblImportanceRatio and i need this column to generate those number in every row 25,20,35,20
this is the Gridview:
enter image description here
and the column name SymbolicRate i wanna just generate those number 25,20,35,20 in every row i tried to add the generate number in the Grirdview row data bound in for loop but it only give me the last number in the all column i'm Using Asp.net C#
this the Gridview:
<asp:GridView ID="gvRptBattalion" runat="server" AutoGenerateColumns="False" CssClass="table" OnRowDataBound="gvRptBattalion_RowDataBound" Visible="false">
<Columns>
<asp:BoundField DataField="ReadinessTypeName" HeaderText="Type" />
<asp:BoundField DataField="SymbolicRate" HeaderText="Rate" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" Text="SymbolicRate"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblImportanceRatio" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" Text="Total"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:BoundField DataField="BattalionDate" HeaderText="#">
<FooterStyle CssClass="hide_identity" />
<HeaderStyle CssClass="hide_identity" HorizontalAlign="Right" />
<ItemStyle CssClass="hide_identity" />
</asp:BoundField>
<asp:BoundField DataField="SUnitId" HeaderText="#">
<FooterStyle CssClass="hide_identity" />
<HeaderStyle CssClass="hide_identity" HorizontalAlign="Right" />
<ItemStyle CssClass="hide_identity" />
</asp:BoundField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" Text="TotalAll"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblFormla" runat="server"></asp:Label>
</ItemTemplate>
<FooterStyle CssClass="lbl" />
<HeaderStyle CssClass="lbl" HorizontalAlign="Right" />
<ItemStyle CssClass="lbl" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" Text="TotalALL2"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblG" runat="server"></asp:Label>
</ItemTemplate>
<FooterStyle CssClass="lbl" />
<HeaderStyle CssClass="lbl" HorizontalAlign="Right" />
<ItemStyle CssClass="lbl" />
</asp:TemplateField>
</Columns>
</asp:GridView>
I have gridview with footer and addbutton on that, how to add new rows if I push the addbutton ? so I can enter new data on that. I also want save them all into database, can somebody explain please
this my front code:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" ShowFooter="True" Width="707px" >
<AlternatingRowStyle BackColor="White"/>
<Columns>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="AddProduct" runat="server" CssClass="button" Text="Add" onclick="AddProduct_Click" ></asp:Button>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Id Trans" DataField="Id_Trans" ShowHeader="False" Visible="False">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Nama Barang" SortExpression="Nama_Item">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="tampilbarang" DataTextField="Nama_Item" DataValueField="Id_Item">
</asp:DropDownList>
<asp:SqlDataSource ID="tampilbarang" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Item]"></asp:SqlDataSource>
</ItemTemplate>
<HeaderStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Keterangan Penggunaan">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Aksi">
<ItemTemplate>
<asp:Button ID="Del" runat="server" Height="22px" Text="Delete" Width="57px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
There are a lot of things to explain in answer your question, so instead I'd recommend a good tutorial - try this - https://quickstarts.asp.net/quickstartv20/aspnet/doc/ctrlref/data/gridview.aspx
You can add a button in footer template like this example:
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
I have a GridView defined like this:
<asp:GridView ID="GridView_Messages" runat="server" AutoGenerateColumns="False" PageSize="5" CellPadding="4" AllowPaging="true" Width=100% AllowSorting="true" OnSorting="GridView_Messages_Sorting"
ForeColor="#333333" GridLines="None" OnRowDataBound="GridView_Messages_OnRowDataBound" OnPageIndexChanging="GridView_Messages_PageIndexChanged" OnRowCommand="GridView_Messages_OnRowCommand" EmptyDataText="<%$ Resources:Localization, NoMsg %>">
<%--CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"--%>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<HeaderTemplate>
<asp:CheckBox ID="allchk" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="check" Text="" TextAlign="Right" AutoPostBack="false" Checked="false" runat="server" OnCheckedChanged="GridView_Messages_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MessageID" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblMessageID" runat="server" Text='<%# Eval("MessageID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, Title2 %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.Title">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblTitle" runat="server" Text='<%# Eval("Title") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblDescription" runat="server" Text='<%# Eval("Description")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FromUserID" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblFromUserID" runat="server" Text='<%# Eval("FromUserID")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, From %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.UserName">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblFromUserName" runat="server" Text='<%# Eval("UserName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblNotes" runat="server" Text='<%# Eval("Notes") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, IsRead %>" Visible="false" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.IsRead">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblIsRead" runat="server" Text='<%# Eval("IsRead") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, TimeSent %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.SentDateTime">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblDateTime" runat="server" Text='<%# Eval("SentDateTime") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Msg_lnkBtnClk" runat="server" CommandName="SingleClick" Text="click"
Visible="true" CssClass="hidden"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Msg_lnkBtnDblClk" runat="server" CommandName="DoubleClick" Text="dblClick"
Visible="true" CssClass="hidden"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#C7B88D" />
</asp:GridView>
I don't want my OnRowCommand to execute for the first column of my GridView. Actually I don't want my event to fire when some of my checkboxes is clicked. How to determine if I have clicked on a checkbox or I have clicked somewhere else in a gridview? In WPF I could use e.OriginalSource property but here this is not possible.
Using JQuery:
If you haven't already added JQuery to your page, add this tag to your page's head section:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
And this is the actual code. Add this after the link to JQuery, in the .aspx page's head section:
<script type="text/javascript">
$(function(){
$('#<%= GridView_Messages.ClientID %> input[type="checkbox"]')
.click(function(event){
event.stopPropagation();
event.preventDefault();
})
});
</script>
I am trying to get the required field validation to fire only for the specific row when the add button is clicked. But right now, when I click any add button, it fires the required field validation for all nested grid rows. The code for the nested gridview and the image of the gridviews is the following. I also tried to use validation groups, attempting to get unique validation groups for each nested gridview row, but couldn't get it to work.
<asp:GridView ID="GrdXML" runat="server" AllowSorting="false"
AutoGenerateColumns="False" CellPadding="0"
ForeColor="#333333" GridLines="Horizontal" onrowdeleting="GrdXML_RowDeleting" OnRowDataBound="OnRowDataBound"
ShowFooter="false" CssClass="xmlgrid" OnSorting="GrdXML_Sorting" DataKeyNames="ContractRecordID">
<Columns>
<asp:TemplateField HeaderText="Contract Start Date" SortExpression="ContractStartDate">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ContractStartDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Contract End Date" SortExpression="ContractEndDate">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ContractEndDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server"
CommandArgument='<%# Eval("ContractRecordID") %>' CommandName="Delete"
onclick="BtnDelete_Click" Text="Delete" Width="60px" />
<ajaxToolkit:ConfirmButtonExtender ID="BtnDelete_ConfirmButtonExtender"
runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True"
TargetControlID="BtnDelete">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal runat="server" ID="lit1" Text="<tr id='trGrid'><td colspan='100%'>" />
<asp:GridView ID="projectCodeGridView" runat="server" AllowSorting="false" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="false" ShowFooter="true" OnRowCommand="ProjectCodeGridView_RowCommand" OnRowCreated="ProjectCodeGridView_RowCreated" OnRowDataBound="ProjectCodeGridView_RowDataBound" OnRowDeleting="ProjectCodeGridView_RowDeleting" DataKeyNames="ContractProjectCodeID" BorderStyle="None" BorderWidth="0">
<Columns>
<asp:TemplateField HeaderText="Project Codes" SortExpression="ContractProjectCode" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ContractProjectCode") %>' CssClass="projectcodelabel"></asp:Label>
</ItemTemplate>
<ItemStyle Width="100%" />
<FooterTemplate>
<asp:TextBox ID="projectCodeTextBox" runat="server" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="projectCodeRequiredFieldValidator" runat="server"
ControlToValidate="projectCodeTextBox" ErrorMessage="Project Code Required" ForeColor="Red" Display="Static" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>' Enabled="false"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="100%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete1" runat="server"
CommandArgument='<%# Eval("ContractProjectCodeID") %>' CommandName="Delete"
onclick="BtnDelete1_Click" Text="Delete" Width="60px" />
<ajaxToolkit:ConfirmButtonExtender ID="BtnDelete1_ConfirmButtonExtender"
runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True"
TargetControlID="BtnDelete1">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnAdd" runat="server" onclick="BtnAdd_Click" Text="Add" Width="60px" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>' CommandName="Add" />
</FooterTemplate>
<FooterStyle Width="100%" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#F7F6F3" Font-Bold="True" />
</asp:GridView>
<asp:Literal runat="server" ID="lit2" Text="</td></tr>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="Black" CssClass="padding" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" CssClass="padding" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
For the validation control of each row you are adding the ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>'. As you are saying, that the validation triggers for the wrong row, it could be that ContractProjectCodeID may not be unique for each row. I think what you can do is add row index to the validation group
ValidationGroup='<%# "PC" + Container.DataItemIndex + Eval("ContractProjectCodeID")
I have a grid that is data bound to a generic list. Which I edit and need to persist the changes of, to the db.
The problem is when I hit the submit button it does a PostBack. And sets the data source of the gridview to null.
I've tried storing the data source in the view state ViewState["data"] which works great but the data isn't being updated in the ViewState when changes are made on the grid.
Any ideas?
Here is the code for the GridView:
<asp:GridView ID="dgMerchantConfiguration" runat="server"
AutoGenerateColumns="False"
CausesValidation="False" CustomPagerCssClass="" HeaderHeight="30px" NumberOfPagesHeight="15px"
NumberOfPagesWidth="40px" PagerHeight="15px" PagerWidth="40px" ShowDataGridCustomPaging="None"
SortHeight="15px" SortWidth="70px" BackColor="White" BorderColor="#999999" BorderWidth="1px"
CellPadding="1" Font-Size="Smaller" GridLines="Vertical" Width="100%"
onrowdatabound="dgMerchantConfiguration_RowDataBound" ViewStateMode="Enabled" EnableViewState="False">
<SelectedRowStyle Font-Bold="True" ForeColor="White" BackColor="#f80000"></SelectedRowStyle>
<AlternatingRowStyle BackColor="Gainsboro"></AlternatingRowStyle>
<RowStyle ForeColor="Black" BackColor="#EEEEEE"></RowStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#bd0000" HorizontalAlign="Center"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="CommissionType" HeaderText="Commission Type">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Select">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked='<%# Bind("Selected") %>' OnCheckedChanged="chkSelect_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Override">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkOverride" runat="server" Checked='<%# Bind("Default") %>' OnCheckedChanged="chkOverride_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comission Structure">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="drpCommissionStructure" runat="server" Width="200px" OnSelectedIndexChanged="drpCommissionStructure_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Min">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtMin" runat="server" Width="50px" Text='<%# Bind("Min") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Max">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtMax" runat="server" Width="50px" Text='<%# Bind("Max") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fixed">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtFixed" runat="server" Width="50px" Text='<%# Bind("Fixed") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost Per Unit">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtCostPerUnit" runat="server" Width="50px" Text='<%# Bind("CostPerUnit") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Value">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtUnitValue" runat="server" Width="50px" Text='<%# Bind("UnitValue") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Excluded Amount">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtExclAmount" runat="server" Width="50px" Text='<%# Bind("ExcludedAmount") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtPercentage" runat="server" Width="50px" Text='<%# Bind("Percentage") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And how the data source is set:
protected void ddlMerchants_SelectedIndexChanged(object sender, EventArgs e)
{
// Load the selected businesses config from the db
var t = client.GetMerchantCommissionStructure(new GetMerchantCommissionStructureRequest() { MerchantID = Int32.Parse(ddlMerchants.SelectedItem.Value) });
ViewState["data"] = t.Configurations;
dgMerchantConfiguration.DataSource = t.Configurations;
dgMerchantConfiguration.DataBind();
}
Thanks in Advance!