Asp.net Webforms GridView on row click - c#

I am trying to do a thing, so, when I click on the GridView Row I want to send the CommandArgument, for now, I am doing with linkbuttons and I want to get rid of that, I want the user to click only the row and then it sends the CommanArgument.
At the moment this is my code:
<asp:GridView ID="jj" runat="server" AllowPaging="true" AllowSorting="false"
AutoGenerateColumns="false" CssClass="mGrid" GridLines="None" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" OnPageIndexChanging="gridView_PageIndexChanging" PageSize="10" EmptyDataText="Não existem dados para mostrar">
<Columns>
<asp:TemplateField HeaderText="Pré Ordens">
<ItemTemplate>
<asp:LinkButton ID="PreOrdens" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("TIPO_Registo") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descrição do Erro">
<ItemTemplate>
<asp:LinkButton ID="DescricaoErro" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("Erro") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NUV">
<ItemTemplate>
<asp:LinkButton ID="NUV" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("NUV") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TV">
<ItemTemplate>
<asp:LinkButton ID="ErroID" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("TV") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the C#:
protected void SEND_ID_REGISTO(object sender, EventArgs e)
{
id = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}
This is not the full code, is just the necessary, but yea, I need to send the command argument on the row click not on the linkbutton click.

you must use the RowCreated-Event of the GridView and You must add this on every postback . .
For example
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}

Related

Insert Grid View column value into Textbox and dropdown on button control

I want to Fill gridview column value into given control
something like Project Title value fill inside project Title text box Problem ID fill inside Selected Problem Dropdown and so on... on button click even
i have taken as control name
Project title as txtProjectTitle,
selectProblem Id as ddlSelectProblem,
Project_Start_Date as txtProjectStartDate,
Project_Target_Date as TextBox1,
gridview as GrdTemp,
Procedd button as Button2_Click
ASPX CODE:
[![<asp:GridView ID="GrdTemp" runat="server" Style="width: 100%; text-align: center" class="table table-striped table-bordered" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="S.No." HeaderStyle-Width="5%">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("ID") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Title">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Title") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Problem ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Problem") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Start Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Start_Date") %>' ID="lblID</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Target Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Target_Date") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>][2]][2]
C# code:
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow row = (sender as Label).NamingContainer as GridViewRow;
TextBox txtProject = row.FindControl("txtProjectTitle") as TextBox;
txtProject.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Title"]);
DropDownList ddlProblem = row.FindControl("ddlSelectProblem") as DropDownList;
ddlSelectProblem.SelectedItem.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Problem"]);
txtProjectStartDate.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Start_Date"]);
TextBox1.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Target_Date"]);
}

Remove Hyperlink of specif cell in gridview

I've created a gridview, which all values (footer included) are hyperlinks, to export a detail in Excel.
All is working fine, except, when the value is 0, I don't want to permit the hyperlink, so it doesn't create a empty excel.
<asp:GridView ID="dtlist" runat="server" CellPadding="0" CssClass="table tabela caixa " CellSpacing="0" OnRowDataBound="dtlist_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" >
<Columns >
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LabelT" runat="server" Text="Total"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<asp:HyperLink runat="server" ID="lnkA" NavigateUrl='<%# String.Format("/Export?name={0}, Eval("name")) %>' Text='<%# Eval("value","{0:#####,##0.00 €}") %>' />
<asp:Label runat="server" ID="lblA" Text='<%# Eval("value","{0:#####,##0.00 €}") %>' Visible="false" />
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterTemplate>
<asp:HyperLink runat="server" ID="lnkTA" />
</FooterTemplate>
<FooterStyle CssClass="alinha-direita" />
<HeaderStyle CssClass="alinha-meio" />
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the RowDataBound:
protected void dtlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Total = 0;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hA = (HyperLink)e.Row.FindControl("lnkA");
Label lA = (Label)e.Row.FindControl("lblA");
if (hA.Text.ToDecimal() == 0)
{
hA.Visible = false;
lA.Visible = true;
}
Total += ((DataRowView)e.Row.DataItem).Row["Ano0"].ToDecimal();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
HyperLink tA = (HyperLink)e.Row.FindControl("lnkTA");
tA.NavigateUrl = String.Format("/ExportStock?name={0}","ZZZZ");
tA.Text = Total0.StringEuro();
}
}
I've tried to ask on the e.Row.RowType == DataControlRowType.DataRow, if the value of the Hyperlink is 0, but if one value on the column is 0 it disables all hyperlinks on that column.
How can I remove only for the 0 cell?
Thanks.
CODE UPDATED

FindControl in gridview returns null

I am currently in a dilemma with my gridview not returning a label, which is within a detailsview...
My C# code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// get pet number for when removing a pet from reservation
int numberSelected = -1;
String numbertxt = "-1";
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.Rows[gv1.SelectedRow.RowIndex];
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
// find selected index, and get number in column 0
// label within GridView1 within dvReservation DetailsView
numbertxt = numberLbl.Text;
...
Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="dsObjGet"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField InsertVisible="False" ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField SelectText="Remove" ShowSelectButton="True"
CausesValidation="False">
<ControlStyle CssClass="link" />
</asp:CommandField>
</Columns>
</asp:GridView>
When I breakpoint
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
the label comes out as null (numberLbl)...
The message returned from the exception is:
"Object reference not set to an instance of an object"
EDIT:
This seems to be resolved if I generate lblNumber in an external gridview (on the page) with Eval("NUMBER"), though I don't see why it doesn't work in the current GridView I was trying to work with, given that GridView1 is within a DetailsView.
You should not use the Cell Collection when using FindControl. Just use this
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.SelectedRow;
Label numberLbl = (Label)rvRow.FindControl("lblNumber");

How to enable column in GridView?

I've a ASP.NET GridView with the following data:
Rows will be disable OnRowDataBound based on the value on column3.
GridView :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
<asp:HyperLink ID="hyperlink" runat="server" Text='<% #Eval("Dosage") %>' NavigateUrl='<% #Eval("Dosage") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Drug") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("Patient") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column4">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<% #Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
RowDataBound :
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label a = e.Row.FindControl("Label3") as Label;
if (a.Text == "Sam")
{
e.Row.Enabled = false;
e.Row.Cells[0].Enabled = true;
}
}
}
however, I want column1 always enable, hyperlink in column1 should always clickable.
I've tried get the cells and enabled it, but it is not working.
kindly advise what is the workaround for the issue above.
You can do this by enable/disable particular cell.
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label a = e.Row.FindControl("Label3") as Label;
if (a.Text == "Sam")
{
e.Row.Cells[0].Enabled = true;
e.Row.Cells[1].Enabled = false;
e.Row.Cells[2].Enabled = false;
e.Row.Cells[3].Enabled = false;
}
}
}

Select any particular data from the gridview and dislay it in the label

hai pupils
I am doing a project on online education. In my project i display the trainees/trainers in in a gridview. If i select any data from the gridview it has to be dispalyed in the label kept below. How can i do this?
Thanks in advance for those who answers.
Use asp:LinkButton to show data in each column. Set commandArgument as the data. set same comandname and handle rowcommand in gridview rowcommand event.
try the following:
in aspx page:
<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server">
<Columns>
<asp:TemplateField HeaderText="CategoryID">
<ItemTemplate>
<asp:LinkButton ID="lnkID" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName">
<ItemTemplate>
<asp:LinkButton ID="lnkName" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblCat" runat="server"></asp:Label>
in aspx.cs page:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "sel")
{
lblCat.Text = e.CommandArgument;
}
}

Categories