Get Gridview Datakey from nested gridview - c#

I've got a gridview nested inside another gridview. What I need is that on the inner gridview get the DataKey from the outer gridview.
How can I pass it?
Thanks.
UPDATE
<asp:GridView ID="gvPeople" runat="server" AutoGenerateColumns="false" CssClass="Grid" DataKeyNames="person_id" OnRowDataBound="gvPeople_RowDataBound">
<Columns>
<asp:TemplateField HeaderStyle-Width="10px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/glyphicons/png/glyphicons_236_zoom_in.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvDocuments" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid" DataKeyNames="document_id" ShowFooter="true"
OnRowCommand="gvDocuments_RowCommand" OnRowDataBound="gvDocuments_RowDataBound" OnRowDeleted="gvDocuments_RowDeleted" OnRowDeleting="gvDocuments_RowDeleting"
GridLines="Horizontal" >
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="document_date" HeaderText="Document Date" />
<asp:BoundField ItemStyle-Width="100px" DataField="value" HeaderText="Value" DataFormatString="{0:#,##.00 €}" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button runat="server" ID="delbutton" CommandArgument='<%# Eval("document_id") %>' CommandName="Delete" Text="Delete" />
<asp:Button runat="server" ID="editbutton" CommandArgument='<%# Eval("document_id") %>' Text="Edit" UseSubmitBehavior="false" />
</ItemTemplate>
<FooterTemplate>
<asp:Button runat="server" ID="newdoc" CommandName="New" Text="New Document" UseSubmitBehavior="false" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="ndocs" HeaderText="Nº of Documents" />
<asp:BoundField ItemStyle-Width="150px" DataField="total_cost" HeaderText="Total Cost" DataFormatString="{0:#,#0.00 €}" />
</Columns>
</asp:GridView>
Here is my aspx code, which is all working, with the excpetion of the new document button, which runs a javascript button, as the edit button, but on this one I need to pass the person_id, from the row of the gvPeople, so when I create a new gvDocuments row, it will make it to that person id.
Thanks.

Just put it to work.
Add a hiddenfield before the child gridview with the value that I want.
On the RowDataBound of the child gridview, added this:
string str= ((HiddenField)e.Row.Parent.Parent.Parent.FindControl("hf")).Value.ToString();
And now use the str value for anything that I want.

You need to first access each of the parent gridview row then access the child gridview row then access the datakey.
Example:
foreach (GridViewRow rowPeople in gvPeople.Rows)
{
GridView gvDocuments = (GridView)rowPeople.FindControl("gvDocuments");
//this will get you the outer/parent gridview datakeys
gvDocuments.DataKeys[rowPeople.RowIndex].Values[0].ToString();
foreach (GridViewRow rowDocuments in gvDocuments.Rows)
{
//this will get you the inner/child gridview datakeys
gvDocuments.DataKeys[rowDocuments.RowIndex].Values[0].ToString();
}
}
Let me know if that works for you!

Related

GridView adding column with button

I need to add column with button in GridView ? i tied with asp:button i got error also i tried asp:ButtonField i got this error:
"Error Creating Control - narudzbaGridType 'System.Web.UI.WebControls.ButtonField' does not have a public property named 'ID'.
but i gave ID name to my Button field asp:ButtonField ID="example"
<asp:GridView ID="narudzbaGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Let" HeaderText="Let"/>
<%--<asp:BoundField DataField="Kolicina" HeaderText="Kolicina"/>--%>
</Columns>
</asp:GridView>
You can use TemplateField like this (add to columns block):
<asp:templatefield headertext="Author Name">
<itemtemplate>
<asp:button id="buttonl"
Text= 'Click Me'
runat="server"/>
</itemtemplate>
</asp:templatefield>
Hi you need to add an TemplateField. Everybody like use ImageButton but if you want use other control go ahead.
<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:ImageButton ID="imgBtnEditar" runat="server" ImageUrl="~/iconos/Image.png" CommandName="edit" ToolTip="Edit">
</asp:ImageButton>
</ItemTemplate>
<ItemStyle Height="8px"></ItemStyle>
</asp:TemplateField>

How do I Pass Multiple Selected Datagrid Values to Another Page in asp.net?

I have multiple asp.net data grids that a user can select a checkbox to the corresponding value that they want, put in the amount they want in a textbox and submit the request. How do I show what values they requested with the amount on the next page? I can do this with a session I believe but I'm having a hard time finding good examples for something like this. Since they can select multiple values I can't use a query string right? I'm using VB.net but if you answer in c# that's fine. Thanks!
<asp:GridView ID="flexGridView" DataKeyNames="ID" runat="server" AutoGenerateColumns="False" DataSourceID="FormSqlDataSource" CssClass="gridView" ClientIDMode="Static">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="flexCheckBoxList" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Form" ShowHeader="False" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" ShowHeader="False" Visible="False" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty" >
<ItemTemplate>
<asp:TextBox ID="flexTextBox" runat="server" Width="40" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use Session or query string, but I would recommend Session, as there are limitations on the length of a query string, especially if you are not sure how many check boxes might be checked.
Use the OnCheckChanged event of the check box control and set AutoPostBack to true in your template field, like this:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="flexCheckBoxList" runat="server"
AutoPostBack="True" OnCheckedChanged="Check_Clicked" />
</ItemTemplate>
</asp:TemplateField>
protected void Check_Clicked(Object sender, EventArgs e)
{
// Store the check box name, ID or whatever unique value you want in Session here
CheckBox theCheckBox = sender as CheckBox;
// Was the check box found?
if(theCheckBox != null)
{
// Store in Session
Session["CheckBoxValue"] = theCheckBox.SomePropertyValue;
}
}
Then in the Page_Load of your redirect page, you will need to read out the Session value for the checked check boxes.

Adding a button to the last column of a GridView

This is how i created a grid view in my ASP.NET C# project. This grid has 4 columns. I want to add a 5th column and add a button on each and every row. How can i do this ?
<asp:GridView ID="gv" runat="server" CellPadding="1" Width="900px"/>
Set AutoGenerateColumns="False" in the gridview markup
Define columns as BoundField (or TemplateField if you wish)
Add aTemplateField for the button in the last column
Sum up:
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px">
<Columns>
<%-- <asp:BoundField /> Definitions here --%>
...
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Click ME" runat="server" ID="btn" OnClick="Clicked" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
suppose you are binding data table having 4 column say col1,col2,col3 and col4
then your gird view in .aspx will be like;
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px" OnRowCommand="gv_RowCommand">
<Columns>
<asp:TemplateField HeaderText="col1">
<ItemTemplate>
<%#Eval("col1")%>// "col1" is field of your database
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col2">
<ItemTemplate>
<%#Eval("col2")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col3">
<ItemTemplate>
<%#Eval("col3")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col4">
<ItemTemplate>
<%#Eval("col4")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnOK" runat="server" Text="OK" CommandName="show" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
*.cs page will be *
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "show")
{
//your code on click event
}
}

When i Check the Checkbox how do I change that specific row style? C# ASP.NET

So i got this gridview on ASP.NET, it is about a gmail client, i made a template field with a check box that is for select several message at the same time, but first when you select a message i want to change the row background color, I already have my CSS but i dont know how to trigger the action or how to know what row was selected in the code. Here is the template of my grid:
<asp:GridView ID="inboxGrid" runat="server"
AutoGenerateColumns="false" CssClass="inbox" ShowHeader="false" BorderStyle="None" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<ItemStyle Width="24" CssClass="check"/>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="from" />
<ItemTemplate>
<asp:Label ID="lblFrom" runat="server"
Text = '<%# Eval("From") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="subject" />
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%# Eval("Subject") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="date" />
<ItemTemplate>
<asp:Label ID="lblFecha" runat="server" Text='<%# Eval("Fecha") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="inbox"/>
</asp:GridView>
Here is a solution using JQuery
Provided you add a CssClass named chk to asp:checkbox
<asp:CheckBox ID="chkSelect" runat="server" CssClass="chk" />
Here is the script
$(document).ready(function () {
$(".chk :checkbox").live("click", function () {
$(this).closest("tr").css("background-color", this.checked ? "#0000FF" : "");
});
});
Try this:
<asp:CheckBox ID="chkSelect" runat="server" OnClick="changeCss(this.id);" />
You can use toggleClass('CssClassName') to toggle the class on checkbox clicks:
function changeCss(ctrlId){
$(ctrlId).parent().parent().toggleClass('check');
}
The hierarchy is tr > td > checkbox, we go reverse to change the css of tr/row.

Edit and update gridview rows on mouse click event

I want to edit and update gridview rows on mouse click event. If the user clicks on a row it should become editable and when he again clicks on the same row it should become uneditable and the changes should be saved.
my gridview looks like:-
<asp:GridView ID="grdexcelsheet" runat="server" AllowPaging="True"
AutoGenerateColumns="False" RowStyle-HorizontalAlign="Left" HorizontalAlign="Center"
onpageindexchanging="grdexcelsheet_PageIndexChanging"
Width = "100%" Height = "100%" Font-Size="Small">
<PagerSettings Position="Bottom" Mode="NextPreviousFirstLast" FirstPageText="First"
LastPageText="Last" NextPageText="Next" PreviousPageText="Prev" PageButtonCount="5" />
<Columns>
<asp:BoundField DataField="SrNo" HeaderText="SrNo" ReadOnly="true" />
<asp:TemplateField HeaderText="CustomerFieldName">
<ItemTemplate>
<asp:Label ID="Label4" Runat="server" Text='<%# Bind("CustomerFieldName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerFieldName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyFieldName" HeaderText="CompanyFieldName" ReadOnly="true" />
<asp:BoundField DataField="Usage" HeaderText="Usage" ReadOnly="true" />
</Columns>
</asp:GridView>
There are many ways to do this. You can use AJAX Control Toolkit Modal Popup Extender for updating your grid. Also, you can make use Update Panel from the same toolkit.
If you are willing to use third party controls...please use this which will be very to use
http://obout.com/grid/grid_commands.aspx...

Categories