change dropdown selected value on rowediting event of parent gridview - asp.net

I have this gridview inside an updatepanel:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" ForeColor="Black"
GridLines="Horizontal" Height="100%" Width="100%" HorizontalAlign="Justify" CssClass="table-responsive-lg table-striped table-hover" CellPadding="5" Style="text-align: center" OnRowEditing="GridView1_RowEditing">
This edit button on the row:
<ItemTemplate>
<asp:LinkButton ID="btnSelect" runat="server" CausesValidation="False" CommandName="Edit"
ToolTip="Seleziona"><i class="far fa-hand-point-up fa-3x" aria-hidden="true"></i></asp:LinkButton>
</ItemTemplate>
And this dropdown inside an EditItemTemplate:
<asp:TemplateField HeaderText="Mittente" SortExpression="Mittente">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#: Bind("Mittente") %>' CssClass="content"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<div style="text-align: left">
<div class="wrapper">
<asp:Button ID="btnPrendiMail" runat="server" Text="Prendi" OnClick="btnPrendiMail_Click" CssClass="btn btn-default" />
<asp:SqlDataSource ID="sqlClienti" runat="server"
ConnectionString="<%$ ConnectionStrings:dbVulcanoConnectionString %>"
SelectCommand="select idcliente,RagSociale,concat(ragsociale,' [' ,idcliente, ']') as Descr from clienti order by RagSociale"></asp:SqlDataSource>
<asp:DropDownList ClientIDMode="Static" OnSelectedIndexChanged="ddlClienti_SelectedIndexChanged" ID="ddlClienti" runat="server" Font-Size="X-Large" Font-Bold="True" AutoPostBack="true" DataSourceID="sqlClienti" DataTextField="Descr" DataValueField="idCliente" AppendDataBoundItems="true" CssClass="select2-single form-control-div" >
<asp:ListItem Text="-- Cerca Cliente per ID o Ragione Sociale --" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
</div>
<asp:Label ID="lblEmailMittente" ClientIDMode="Static" runat="server" Text='<%#: Bind("Mittente") %>' CssClass="content"
Visible="false"></asp:Label>
</div>
</EditItemTemplate>
<ItemStyle Width="10%" />
</asp:TemplateField>
I would to change the selected value of the dropdown when the Edit button is pressed, executing this subroutine on RowEditing event of the gridview:
Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
Dim gvRow As GridViewRow = GridView1.Rows.Item(e.NewEditIndex())
Dim ddlClienti As DropDownList = TryCast(gvRow.FindControl("ddlClienti"), DropDownList)
Dim lblEmailMittente As Label = TryCast(gvRow.FindControl("lblEmailMittente"), Label)
If dbVulcano.emails.Any(Function(m) m.email = lblEmailMittente.Text) Then
Dim email As emails = dbVulcano.emails.Where(Function(m) m.email = lblEmailMittente.Text).SingleOrDefault
ddlClienti.SelectedValue = email.rfCliente
ddlClientiIndexChanged()
End If
End Sub
But while debugging it I see that ddlClienti is = Nothing so the routine fails.
What is the right way to do this? Thanks

I did the same thing using a timer inside the template field

Related

How to Enable a DropDownList in Gridview When Selected the First DropDownList

I'm working with my GridView table in ASP.Net.
First when I click the Edit link button in my GridView I will be able to edit it with Textbox and dropdownlist.
Here's the image of my GridView.
So the Business Unit was enabled but the Division, Sub-Division, Classification and Sub-Classification are not.
My problem is that when I select the Business Unit, the dropdownlist Division should be enabled and be able to select the value according to the Business Unit that I selected and so on. So the next dropdownlist will be dependent on the first dropdownlist that you selected. I searched about this on the internet already and tried different solutions but I'm still confused and don't know how it will work.
Here's my code in ASP.Net
<asp:GridView ID="gvGroup" runat="server" AllowPaging="false"
AllowSorting="true" AutoGenerateColumns="False" BorderColor="Silver"
BorderWidth="1px" Height="104px" ShowFooter="true" ShowHeader="true"
style="margin-right: 0px" Width="77%">
<RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderStyle-BackColor="#666666"
HeaderStyle-ForeColor="White" HeaderStyle-Width="10px" HeaderText=""
ItemStyle-Width="10px">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle BackColor="#666666" ForeColor="White" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblMasterID" runat="server" Text='<%#Bind("MASTERID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblSEQID" runat="server" Text='<%#Bind("SEQID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Job Title" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblJobTitle" runat="server" Text='<%#Bind("JOBTITLE") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJT" runat="server" CssClass="ehr_textbox"
Text='<%#BIND("JOBTITLE") %>' Width="140px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtJobTitleAdd" runat="server" Width="110px">
</asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Business Unit"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit" runat="server"
Text='<%#Bind("BUSINESS_UNIT") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBusinessUnitAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Division" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblDivision" runat="server" Text='<%#Bind("DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Division"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubDivision" runat="server" Text='<%#Bind("SUB_DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server"
Text='<%#Bind("CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubClassification" runat="server"
Text='<%#Bind("SUB_CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" ItemStyle-Font-Size="9pt">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandName="Delete"
OnClientClick="return confirm('Do you want to delete?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnAdd" runat="server" CommandName="Add"
OnClick="AddgvGroup" Text="Add"></asp:LinkButton>
</FooterTemplate>
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:TemplateField>
<asp:CommandField HeaderStyle-BackColor="#666666" HeaderStyle-Width="10px"
ItemStyle-Font-Size="9pt" ItemStyle-Width="10px" ShowEditButton="True">
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
<EmptyDataTemplate>
<div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
No record found.
</div>
</EmptyDataTemplate>
<HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial"
Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
And here's my code in VB.Net
Protected Sub gvGroup_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGroup.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlBusinessUnit As DropDownList = DirectCast(e.Row.FindControl("ddl_gvBusinessUnit"), DropDownList)
'bind dropdown-list
Dim sqlstr As String
Dim dt As DataTable = New DataTable()
sqlstr = "Select BUSINESS_UNIT from BUSINESS_UNIT_TBL"
dt = ehr_utils.DataTable(sqlstr)
ddlBusinessUnit.DataSource = dt
ddlBusinessUnit.DataTextField = "BUSINESS_UNIT"
ddlBusinessUnit.DataValueField = "BUSINESS_UNIT"
ddlBusinessUnit.DataBind()
Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
ddlBusinessUnit.SelectedValue = dr("BUSINESS_UNIT").ToString()
End If
End If
End Sub
Any idea of how can I possibly do that? And I tried also the OnSelectedIndexChanged
First you need to add an OnSelectedIndexChanged to ddl_gvBusinessUnit and set AutoPostBack to true
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_gvBusinessUnit_SelectedIndexChanged">
And then in code behind
protected void ddl_gvBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList dropDownList1 = sender as DropDownList;
//get the selectedvalue
string value = dropDownList1.SelectedValue;
//find the other dropdownlist in the correct row by using the editindex
DropDownList dropDownList2 = gvBusinessUnit.Rows[gvBusinessUnit.EditIndex].FindControl("DropDownList2") as DropDownList;
//do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red;
}
VB
Protected Sub ddl_gvBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
'cast the sender back to a dropdownlist
Dim dropDownList1 As DropDownList = CType(sender,DropDownList)
'get the selectedvalue
Dim value As String = dropDownList1.SelectedValue
'find the other dropdownlist in the correct row by using the editindex
Dim dropDownList2 As DropDownList = CType(gvBusinessUnit.Rows(gvBusinessUnit.EditIndex).FindControl("DropDownList2"),DropDownList)
'do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red
End Sub

Selected row to textbox when clicked on row not firing

I can click on row to selected them but when i need them to textbox it's not firing i tried to use Autopostback on textbox but it's doesn't work i think it's because of for each that not make row.cells(0) work
i write a code like this
VB.NET
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
If row.RowIndex = GridView1.SelectedIndex Then
row.BackColor = ColorTranslator.FromHtml("#E294FF")
txtrepname.Text = row.Cells(0).Text
row.ToolTip = String.Empty
Else
row.BackColor = ColorTranslator.FromHtml("#FFFFFF")
row.ToolTip = "Click to select this row."
End If
Next
End Sub
Gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" ShowFooter="True" Width="1280px" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged"
onrowupdating="GridView1_RowUpdating" DataKeyNames="REP_NAME" PageSize="9999">
<Columns>
<asp:TemplateField HeaderText="" >
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="" CommandName="Edit" ToolTip="Edit"
CommandArgument=''><img src="Images/ad_edit.png" BORDER="0" /></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
ToolTip="Delete" OnClientClick='return confirm("Are you sure you want to delete this entry?");'
CommandArgument='' BorderColor="#CC3300"><img src="Images/ad_delete.png" BORDER="0" /></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="editGrp" CommandName="Update" ToolTip="Save"
CommandArgument=''><img src="Images/ad_save.png" BORDER="0" /></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="Cancel" ToolTip="Cancel"
CommandArgument=''><img src="Images/ad_reload.png" BORDER="0" /></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="newGrp" CommandName="InsertNew" ToolTip="Add New Entry"
CommandArgument=''><img src="Images/ad_add.png" BORDER="0"/></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="CancelNew" ToolTip="Cancel"
CommandArgument=''><img src="Images/ad_reload.png" BORDER="0"/></asp:LinkButton>
</FooterTemplate>
<HeaderStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Report Name" SortExpression="REP_NAME" >
<ItemTemplate>
<div style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap; min-width:150px; ">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("REP_NAME") %>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("REP_NAME") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtREP_NAMENew" runat="server" CssClass="" Width="60px" ></asp:TextBox>
<asp:RequiredFieldValidator ID="valREP_FILEID" runat="server" ControlToValidate="txtREP_NAMENew"
Display="Dynamic" ErrorMessage="Province is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
<HeaderStyle Width="80px" />
</asp:TemplateField>
</Columns>
</asp:GridView>

Lost DataKey value in ItemDataBound of ListView when paging

I'm binding data to GridView nested in ListView via ItemDataBound and when I'm paging ListView it's loosing datakey value.
Here is DataBound event:
Protected Sub gvResult_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles gvResult.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim dbSrc As New SqlDataSource
dbSrc.SelectParameters.Clear()
Dim gv As GridView = e.Item.FindControl("gvPrices")
Dim currentItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
Dim currentDataKey As DataKey = Me.gvResult.DataKeys(currentItem.DataItemIndex)
dbSrc.ConnectionString = ConfigurationManager.ConnectionStrings("hotelbedsConnectionString").ConnectionString
dbSrc.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
dbSrc.SelectCommand = "GetPriceTest"
dbSrc.SelectParameters.Add(New Parameter("SearchID", DbType.String, CStr(Request.QueryString("SearchID"))))
dbSrc.SelectParameters.Add(New Parameter("HotelCode", DbType.String, CStr(currentDataKey.Value)))
gv.DataSource = dbSrc
AddHandler gv.RowCommand, AddressOf MarkSelected
gv.DataBind()
End If
End Sub
I'm using normal paging control in ListView like this one:
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NumericPagerField ButtonCount="10" CurrentPageLabelCssClass="CurrentPage" NumericButtonCssClass="PageNumbers"
NextPreviousButtonCssClass="PageNumbers" NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
Error is: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Here is full markup of ListView with nested GridView:
<asp:ListView ID="gvResult" runat="server" DataSourceID="ds_hotels" DataKeyNames="HotelCode">
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<ItemTemplate>
<div style="width: 96%; float: left;" class="dashed">
<div style="width: 18%; float: left;">
<asp:HyperLink ID="hlHotelInfo" runat="server" Text="Фотографии и информация" NavigateUrl='<%# Eval("HotelCode", "details/{0}") %>'
ImageUrl='<%# Eval("HotelCode", "hotelfront.ashx?HotelCode={0}") %>' CssClass="img_hotel"></asp:HyperLink></div>
<h2 class="title_hotels">
<asp:HyperLink ID="Label2" runat="server" Text='<%# ShowHotelName(Eval("HotelCode")) %>'
NavigateUrl='<%# Eval("HotelCode", "details/{0}") %>'></asp:HyperLink></h2>
<p>
<asp:Label ID="lblHotelDesc" runat="server" Text='<%# ShowHotelDescr(Eval("HotelCode")) %>' />
</p>
</div>
<asp:GridView runat="server" ID="gvPrices" AutoGenerateColumns="False" GridLines="None"
Width="100%" RowStyle-HorizontalAlign="Center" DataSourceID="ds_prices" DataKeyNames="ID">
<AlternatingRowStyle BackColor="#00FF99" />
<SelectedRowStyle Font-Bold="true" BackColor="BlanchedAlmond" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="addToBasket" runat="server" ImageUrl="~/img/32px/Add.png" ToolTip="Добавить в корзину"
OnCommand="MarkSelected" CommandName="Select" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Стоимость">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# ShowMarkUp(Eval("Price", "{0:#0}")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Currency" HeaderText="Валюта" SortExpression="Currency" />
<asp:BoundField DataField="AdultCount" HeaderText="Взрослых" SortExpression="AdultCount" />
<asp:BoundField DataField="ChildCount" HeaderText="Детей" SortExpression="ChildCount" />
<asp:BoundField DataField="RoomCount" HeaderText="Номеров" SortExpression="RoomCount" />
<asp:BoundField DataField="RoomTypeName" HeaderText="Тип номера" SortExpression="RoomTypename" />
<asp:TemplateField HeaderText="Питание" SortExpression="BoardShortName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("BoardShortName") %>' ToolTip='<%# ShowToolTip(Eval("BoardShortName")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:SqlDataSource ID="ds_prices" runat="server" ConnectionString="<%$ ConnectionStrings:hotelbedsConnectionString %>"
SelectCommand="GetPriceTest" CancelSelectOnNullParameter="False" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="SearchID" Type="String" />
<asp:Parameter Name="HotelCode" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholder" runat="server">
</div>
<br />
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NumericPagerField ButtonCount="10" CurrentPageLabelCssClass="CurrentPage" NumericButtonCssClass="PageNumbers"
NextPreviousButtonCssClass="PageNumbers" NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
</asp:ListView>
Correct question is the half of the answer, yeah...
I shoud rather ask - why there is out of range exception.
The asnwer is here:
currentDataKey = Me.gvResult.DataKeys(currentItem.DisplayIndex)
#Abide, thanks for tryinh to help me!
Make sure that in the data being brought back from the database there is no entry with a null value in your datakey causing a negative index.And try this
Dim currentItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
If currentItem.DataItemIndex < Me.gvResult.Count-1 then
Dim currentDataKey As DataKey = Me.gvResult.DataKeys(currentItem.DataItemIndex)
End if
Dim HotelCode As String = currentDataKey("HotelCode") ' where the value in quotes would be the name of your datakey.

Assign value of nested gridview into CommandName

I have a nested gridview and I want to get the value of the first gridview and assign it to the commandArgument of a button. Can someone advise how to do it
As you will see in the code below I'm already getting the row index with the following code:
"
Text="Save Changes" />
What I need is the DataField="name" instead
See full code below.
<script language="javascript" type="text/javascript">
function divexpandcollapse(divname) {
var div = document.getElementById(divname);
var img = document.getElementById('img' + divname);
if (div.style.display == "none") {
div.style.display = "block"; img.src = "Images/Icons/minus.jpg";
} else { div.style.display = "none"; img.src = "Images/Icons/plus.jpg"; }
}</script>
<asp:DropDownList ID="DateSelection" runat="server" AutoPostBack="True" Height="21px"
Width="134px">
</asp:DropDownList>
<asp:GridView ID="GV_SL" runat="server" OnRowDataBound="gvUserInfo_RowDataBound"
OnRowCommand="GV_SL_RowCommand" AutoGenerateColumns="False"
DataSourceID="SQL_Weekly" AllowSorting="True">
<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("name") %>');">
<img id="imgdiv<%# Eval("name") %>" width="15px" border="0" src="Images/Icons/plus.jpg" /></a></ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="Group" SortExpression="name" />
<asp:BoundField DataField="ASL" HeaderText="SL% Act" ReadOnly="True" SortExpression="ASL" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("name") %>" style="display: none; position: relative; left: 15px;
overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Metric" HeaderText=" " HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Actual" HeaderText="Actual" HeaderStyle-HorizontalAlign="Left" />
</Columns>
</asp:GridView>
<br />
<asp:UpdatePanel ID="UP_SecondPanel" runat="server" UpdateMode="Always" >
<ContentTemplate>
<%-- --%>
<asp:TextBox ID="TB_Comments" runat="server" Text="Example: Text will be entered here"
TextMode="MultiLine" Rows="4" Width="510px"></asp:TextBox>
<asp:Button ID="B_Save" runat="server" CommandName="AddText" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Save Changes" />
</ContentTemplate>
</asp:UpdatePanel>
<%-- --%>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Any ideas will be appreceiated
Thanks
I sorted the problem with VB code. See below if it helps someone
Protected Sub GV_SL_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "AddText") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button
' from the Rows collection.
Dim row As GridViewRow = GV_SL.Rows(index)
Dim GroupName As String = GV_SL.Rows(index).Cells(1).Text
'Rest of the code here
End If
End Sub

Gridview edit not working

I have the following gridview:
<asp:GridView ID="GrdAll" runat="server" AutoGenerateColumns="false" ShowHeader="false"
Width="40%" GridLines="None" CellPadding="2" CellSpacing="2">
<AlternatingRowStyle CssClass="AltColor22" />
<RowStyle CssClass="AltColor21" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" class="TableBorder">
<tr>
<td valign="top" align="left">
<asp:Label ID="lblName" runat="server" />
<hr />
</td>
</tr>
<tr>
<td valign="top" align="left" class="TableBorder">
<asp:GridView ID="GrdContent" runat="server" AutoGenerateColumns="False"
Width="100%" GridLines="None" CellPadding="2" CellSpacing="2"
OnRowDataBound="GrdContent_RowDataBound" OnRowEditing="GrdContent_RowEditing"><%----%>
<Columns>
<asp:TemplateField HeaderText="Kiosk ID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblKiosID" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKiosk" runat="server" Text ='<%#Eval("HBEmailID") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email ID" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text ='<%#Eval("HBEmailID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit"
Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this, edit functionality is not working.
In clicking of edit button, it shows me nothing. (It should show me textbox to edit)
Following is code:
Protected Sub GrdContent_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
Dim row As GridViewRow = DirectCast(TryCast(sender, Control).Parent.Parent, GridViewRow)
Dim index As Integer = row.RowIndex
CType(row.FindControl("GrdContent"), GridView).EditIndex = e.NewEditIndex
DBName = CType(row.FindControl("lblName"), Label).Text
bindContentGrid(CType(row.FindControl("GrdContent"), GridView), DBName)
End Sub
Please help me, it does not gives me error, but it also not showing me textbox to edit.
The issue could be on the following code
bindContentGrid(CType(row.FindControl("GrdContent"), GridView), DBName)
If that code does not return any records, then nothing will be displayed. Make sure it is returning some rows.

Resources