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;
}
}
}
Related
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);
}
}
I have a GridView with Template Fields and Connection String is located in the
web.config File. Data is fetched by a stored procedure. I want to retrieve a single cell value of the Gridview in protected void someMethod(object sender, EventArgs e) on OnClick="someMethod" event. But it ends up with:
Object reference not set to an instance of an object.
Code that generates the OnClick Event:
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="someMethod">Get Name</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
The value of Gridview Cell i want to get is:
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="forNames" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="eForNames" runat="server" Text = '<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And Code Behind is:
protected void someMethod(object sender, EventArgs e)
{
Label ename = diplayAdapter.SelectedRow.FindControl("eForNames") as Label;
Label5.Text = ename.Text;
}
.ASPX Code:
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument="<%# Container.DisplayIndex %>" runat="server" CommandName="FindName">Get Name</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="forNames" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="eForNames" runat="server" Text = '<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
.CS Code
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//getting rowindex which we have selected by using CommandArgument
int rowindex = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "FindName")
{
Label5.Text= GridView1.Rows[rowindex].Cells[1].FindControl("eForNames");
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
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
I have a GridView, which is populated using a linq query on button click.
I have to populate the header text dynamically, fetching from the database. The first time I click the button, the header text is not binding. But, if I click on the button a second time, the header texts are bound.
Markup:
<asp:GridView ID="grdresult" runat="server" AutoGenerateColumns="false"
OnRowDataBound="grdresult_RowDataBound">
<Columns>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol1" runat="server" Text=' <%#Eval("Col1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol2" runat="server" Text='<%#Eval("Col2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Col3") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol4" runat="server" Text=' <%#Eval("Col4") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol5" runat="server" Text='<%#Eval("Col5") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol6" runat="server" Text=' <%#Eval("Col6") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol7" runat="server" Text=' <%#Eval("Col7") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol8" runat="server" Text=' <%#Eval("Col8") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Records Found
</EmptyDataTemplate>
</asp:GridView>
Codebehind:
protected void btnreport_Click(object sender, EventArgs e)
{
BindGrid();
}
protected void BindGrid()
{
var results = "LINQ select query from database";
}).Distinct().ToList();
for (int i = 0; i < grdresult.Columns.Count; i++)
{
grdresult.Columns[i].Visible = true;
}
grdresult.DataSource = results;
grdresult.DataBind();
}
protected void grdresult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
var getColumnHeader = "LINQ query to get header text";
if (getColumnHeader != null)
{
grdresult.Columns[5].HeaderText = getColumnHeader.HCol3;
grdresult.Columns[6].HeaderText = getColumnHeader.HCol4;
grdresult.Columns[7].HeaderText = getColumnHeader.HCol5;
grdresult.Columns[8].HeaderText = getColumnHeader.HCol6;
grdresult.Columns[9].HeaderText = getColumnHeader.HCol7;
grdresult.Columns[10].HeaderText = getColumnHeader.HCol8;
grdresult.Columns[11].HeaderText = getColumnHeader.HCol9;
grdresult.Columns[12].HeaderText = getColumnHeader.HCol10;
}
}
//based on some condition i have set visibility of gridview columns
int TotalColumnCount = (int)ViewState["colcount"];
for (int i = grdresult.Columns.Count - 1; i > TotalColumnCount + 4; i--)
{
grdresult.Columns[i].Visible = false;
}
}
Instead of using the below line for binding header text
grdresult.Columns[5].HeaderText = getColumnHeader.HCol3;
i used below line which solved the issue.
e.Row.Cells[5].Text = getColumnHeader.HCol3;
This error can also occur if you are dumping an integer value into varchar() but the size of varchar is not enough. For example I was getting this error as I was cast sum() to varchar(10), and hence the error.
I changed varchar(10) to varchar(20) and the error got resolved.
In Gridview control set ClientIDMode = "Static"
I can't seem to be able to change the footer text. I've tried the sorted event as well but nothing happens. All I want to do is display status messages. Here is my code:
protected void PageSettings_Sorting(object sender, GridViewSortEventArgs e)
{
if (((GridView)sender).EditIndex > -1)
{
e.Cancel = true;
}
else
{
// tried this on sorted aswell but can't change footer text
GridViewRow row = ((GridView)sender).FooterRow as GridViewRow;
Label lblStatus = new Label{ ID="lblStatus", Text="Sorting Column <b>\"" + e.SortExpression + "\" " + e.SortDirection + "</b>"};
row.Cells[0].Text = "Hello World"; //.Controls.Add(lblStatus);
}
}
protected void PageSettings_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
{
}
else if (e.Row != null && e.Row.RowType == DataControlRowType.Footer)
{
int count = e.Row.Cells.Count;
for (int i = count - 1; i >= 1; i += -1)
{
e.Row.Cells.RemoveAt(i);
}
e.Row.Cells[0].ColumnSpan = count;
e.Row.Cells[0].Controls.Add(new Literal { ID = "lblStatus" });
// can't FindControl or change Literals either
e.Row.Cells[0].Text = "Hello World"; // works here but not on sorting event
}
}
<asp:GridView ID="PageSettings" runat="server"
AllowPaging="true" AllowSorting="true"
AutoGenerateColumns="false"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
ShowFooter="true"
DataSourceID="ObjectDataSourcePages"
OnLoad="PageSettings_Load"
OnRowDataBound="PageSettings_DataBound"
OnRowCreated="PageSettings_RowCreated"
OnRowEditing="PageSettings_RowEditing"
OnRowCancelingEdit="PageSettings_RowCancelingEdit"
OnPageIndexChanging="PageSettings_PageIndexChanging"
OnSorting="PageSettings_Sorting"
OnSorted="PageSetting_Sorted"
PageSize="2">
<Columns>
<asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left" SortExpression="Path">
<ItemTemplate>
<%# Eval("Path") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left" SortExpression="RouteValue">
<ItemTemplate>
<%# Eval("RouteValue") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left" SortExpression="RegExp">
<ItemTemplate>
<%# Eval("RegExp") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This will change the text of the first FooterRow cell:
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
gv.FooterRow.Cells[0].Text = "Hello";
}
Of course you'll need to make sure your GridView's ShowFooter property is true.
Or alternatively, by casting sender and adding a control:
protected void Sorting(object sender, GridViewSortEventArgs e)
{
Label label = new Label();
label.Text = gv_s.Rows.Count.ToString() + " records";
((GridView)sender).FooterRow.Cells[0].Controls.Add(label);
}