I have below code to create a gridview in asp.net and inside gridview I have a delete button. Below code works fine and shows Delete in all rows.
I want to hide/ Disable the Delete button in very first row. Can somebody suggest the code part?
<asp:gridview ID="Gridview1" runat="server"
ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Cat">
<ItemTemplate>
<asp:TextBox ID="TextBoxCat" runat="server" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" >
<ItemTemplate>
<asp:LinkButton ID="DeleteItemsGridRowButton" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
You can use GridView.RowDataBound Event event.
Then find the LinkButton using FindControl method.
public class Animal
{
public int RowNumber { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = new List<Animal>
{
new Animal {RowNumber = 1, Name = "One"},
new Animal {RowNumber = 2, Name = "Two"},
new Animal {RowNumber = 3, Name = "Three"},
new Animal {RowNumber = 4, Name = "Four"},
};
Gridview1.DataBind();
}
}
private int _counter;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (_counter == 0)
{
var linkButton = e.Row.FindControl("DeleteItemsGridRowButton")
as LinkButton;
linkButton.Visible = false;
_counter++;
}
}
}
Try This:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(GridView1.Rows.Count>0)
{
//GridView1.Rows[0].Visible = false;
LinkButton DeleteItemsGridRowButton= (LinkButton) GridView1.Rows[0].FindControl("DeleteItemsGridRowButton");
if(DeleteItemsGridRowButton!=null)
{
DeleteItemsGridRowButton.Visible=false
}
}
}
Related
I have a 2 Gridviews. The first grid has a button that when clicked it will populate a second grid with the data based on the id of the button clicked.
I then have code in the RowDataBound function to show the grid based on the row selected. But the problem is the code is automatically running the RowDataBound before the populate function. So the second grid isn't displaying.
Code for GridView:
<asp:GridView style="width:75%"
ID="gvCVRT"
ShowHeaderWhenEmpty="true"
CssClass="tblResults"
runat="server"
OnRowDataBound="gvCVRT_RowDataBound"
OnSelectedIndexChanged="gridviewParent_SelectedIndexChanged"
DataKeyField="ID"
DataKeyNames="ChecklistID"
AutoGenerateColumns="false"
allowpaging="false"
AlternatingRowStyle-BackColor="#EEEEEE">
<HeaderStyle CssClass="tblResultsHeader" />
<Columns>
<asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField>
<asp:CommandField ShowSelectButton="True" HeaderText="Select" />
<asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField>
<asp:BoundField DataField="User" HeaderText="User" ></asp:BoundField>
<asp:BoundField DataField="Note" HeaderText="Note" ></asp:BoundField>
</Columns>
</asp:GridView>
Code behind:
protected void gvCVRT_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
lookupCVRT work = (lookupCVRT)e.Row.DataItem;
GridView gv = sender as GridView;
if (work.ID != null)
{
int index = gv.Columns.HeaderIndex("Select");
if (index > -1)
{
e.Row.Cells[index].Attributes.Add("class", "gvCVRTRow");
e.Row.Cells[index].ToolTip = "Click here to Edit Checklist";
}
}
}
}
Code for select button:
protected void gridviewParent_SelectedIndexChanged(object sender, EventArgs e)
{
List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), gvCVRT.SelectedDataKey.Value.ToString());
gvCVRTDetails.DataSource = workDetails;
gvCVRTDetails.DataBind();
FireJavascriptCallback("setArgAndPostBack ();");
}
So the problem is when I click on the Select button in the grid it runs the RowDataBound first then the gridviewParent_SelectedIndexChanged but I need to run gridviewParent_SelectedIndexChanged first. Can I call the RowDataBound function from gridviewParent_SelectedIndexChanged?
Page_Load function:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetChecklistID = "";
if (ParentID.HasValue)
{
ViewState["ParentID"] = ParentID;
List<lookupCVRT> work = lookupCVRT.GetCVRTItems(Company.Current.CompanyID, ParentID.ToString());
ViewState["CVRT"] = work;
gvCVRT.DataSource = work;
gvCVRT.DataBind();
}
}
else
{
if (ViewState["ParentID"] != null)
{
ParentID = (int?)ViewState["ParentID"];
List<lookupCVRT> work = ViewState["CVRT"] as List<lookupCVRT>;
gvCVRT.DataSource = work;
gvCVRT.DataBind();
}
}
}
The OnRowDataBound event is only called if the DataBind method for the GridView has been called.
In your specific case, the problem is in Page_Load in the else branch of the Page.IsPostBack condition:
else
{
if (ViewState["ParentID"] != null)
{
ParentID = (int?)ViewState["ParentID"];
List<lookupCVRT> work = ViewState["CVRT"] as List<lookupCVRT>;
gvCVRT.DataSource = work;
gvCVRT.DataBind();
}
}
This code is run for each postback. Unless you reset ViewState["ParentID"] somewhere else in your code, on every postback you bind the GridView gvCVRT again. This is the reason that RowDataBound is called. After finishing Page_Load, the page calls the additional event handlers, in your case gridviewParent_SelectedIndexChanged.
In order to solve this problem, you need to change the code in your Page_Load handler so that there are no calls to DataBind for a postback:
// field moved to class level so that you can access this variable instead of a DataRow in gvCVRT
private List<lookupCVRT> work;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetChecklistID = "";
if (ParentID.HasValue)
{
ViewState["ParentID"] = ParentID;
work = lookupCVRT.GetCVRTItems(Company.Current.CompanyID, ParentID.ToString());
ViewState["CVRT"] = work;
gvCVRT.DataSource = work;
gvCVRT.DataBind();
}
}
else
{
if (ViewState["ParentID"] != null)
{
ParentID = (int?)ViewState["ParentID"];
work = ViewState["CVRT"] as List<lookupCVRT>;
}
}
}
The root cause of your problem is that you need the data in a postback request and that you put these into ViewState["CVRT"] instead of requesting the data anew. In web applications it is pretty common the read the data again for a new request. So you might think about whether you really need to put the data into ViewState or whether you can request them upon a postback from the data source.
Putting the data into ViewState increases the size of the page that is transferred to the client (basically you have the HTML for the GridView and in addition you have the data in ViewState). So requesting them anew would be the better way in most cases.
I don't know why you preferred to use gridviewParent_SelectedIndexChanged then grdParent_RowDataBound ... i have created a simple solution for you .. it could help you ..
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<label>Parent Grid</label>
<asp:GridView ID="grdParent" runat="server" AutoGenerateColumns="false"
DataKeyField="Id" OnRowDataBound="grdParent_RowDataBound" OnRowCommand="grdParent_RowCommand">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ButtonField CommandName="Details" HeaderText="Select" Text="Hello" ButtonType="Link" />
</Columns>
</asp:GridView>
</div>
<div>
<label>child Grid</label>
<asp:GridView ID="grdChild" runat="server" AutoGenerateColumns="false"
DataKeyNames="ChildId" OnRowDataBound="grdChild_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Roll" />
<asp:ImageField HeaderText="Image" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
Codebehind
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<ParentClass> pList = new List<ParentClass>()
{
new ParentClass{Id=5, Name="V"},
new ParentClass{Id=6,Name="VI"},
new ParentClass{Id=7,Name="VII"},
new ParentClass{Id=8,Name="VIII"},
new ParentClass{Id=9,Name="IX"},
new ParentClass{Id=10,Name="X"},
};
grdParent.DataSource = pList;
grdParent.DataBind();
}
}
protected void grdParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == null || e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
ParentClass p = e.Row.DataItem as ParentClass;
var btn = e.Row.Cells[1].Controls[0] as LinkButton;
btn.CommandArgument = p.Id.ToString();
}
protected void grdParent_RowCommand(object sender, GridViewCommandEventArgs e)
{
int parentId = Convert.ToInt32(e.CommandArgument);
var releventStudents = GetRepositary().FindAll(i => i.ParentId == parentId);
grdChild.DataSource = releventStudents;
grdChild.DataBind();
}
protected void grdChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == null || e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
//lookupCVRT work = (lookupCVRT)e.Row.DataItem;
//GridView gv = sender as GridView;
//if (work.ID != null)
//{
// int index = gv.Columns.HeaderIndex("Select");
// if (index > -1)
// {
// e.Row.Cells[index].Attributes.Add("class", "gvCVRTRow");
// e.Row.Cells[index].ToolTip = "Click here to Edit Checklist";
// }
//}
}
private List<ChildClass> GetRepositary()
{
List<ChildClass> allChild = new List<ChildClass>();
Random r = new Random();
for (int i = 0; i < 50; i++)
{
ChildClass c = new ChildClass
{
ChildId = i,
ParentId = r.Next(5, 10),
Name = "Child Name " + i,
Roll = i
};
allChild.Add(c);
}
return allChild;
}
}
public class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ChildClass
{
public int ParentId { get; set; }
public int ChildId { get; set; }
public int Roll { get; set; }
public string Name { get; set; }
}
protected void Gridproducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hp = new HyperLink();
hp = (HyperLink)e.Row.FindControl("linkSelectprd");
var Pid = DataBinder.Eval(e.Row.DataItem, "product_id").ToString();
var Catid = Request.QueryString["Cid"].ToString();
hp.NavigateUrl = "Sales.aspx?Cid="+Catid+"&"+"Pid="+Pid;
if (!IsPostBack && Request.QueryString["Pid"] != null)
{
this is the variable in which the value of quantity increments
int i=0;
lbltotalquantity.Text = i.ToString() + 1;
}
}
}
}
I use LinkButtons inside a Grid template. I want to be able to raise events when clicking the LinkButtons the value of lable is incremented on + link and decrementd on - link button as lable contains the quantity of Products added to invoice. How can I accomplish this?
I believe this is what you want....
You don't do the increment in RowDataBound because RowDataBound trigger when you are binding the GridView
.aspx
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label ID="lblProduct" runat="server" Text='<%# Eval("Product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text="0"></asp:Label>
<asp:LinkButton ID="lbtnPlus" runat="server" Text="+" OnClick="lbtnPlus_Click"></asp:LinkButton>
<asp:LinkButton ID="lbtnMinus" runat="server" Text="-" OnClick="lbtnMinus_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gv" />
</Triggers>
</asp:UpdatePanel>
.cs
protected void Page_Load(object sender, EventArgs e)
{
// Check
if (!IsPostBack)
{
// Variable
string[] product = { "Dell", "Asus", "Acer", "Toshiba", "Fujishu", "VAIO" };
DataTable dt = new DataTable();
dt.Columns.Add("Product");
for (int i = 0; i < product.Length; i++)
dt.Rows.Add(product[i]);
gv.DataSource = dt;
gv.DataBind();
// Dispose
dt.Dispose();
}
}
private void DoTheMath(GridViewRow row, bool isAdd)
{
// Variable
bool isNumber = false;
int currentValue = 0;
// Find Control
Label lblQuantity = row.FindControl("lblQuantity") as Label;
// Check
if (lblQuantity != null)
{
// Check
if (lblQuantity.Text.Trim() != string.Empty)
{
isNumber = int.TryParse(lblQuantity.Text.Trim(), out currentValue);
// Check
if (isNumber)
{
// Is Add
if (isAdd)
currentValue++;
else
{
// Check cannot be less than 0
if (currentValue > 0)
currentValue--;
}
}
// Set to TextBox
lblQuantity.Text = currentValue.ToString();
}
}
}
protected void lbtnPlus_Click(object sender, EventArgs e)
{
// Get
LinkButton lbtn = sender as LinkButton;
GridViewRow row = lbtn.NamingContainer as GridViewRow;
DoTheMath(row, true);
}
protected void lbtnMinus_Click(object sender, EventArgs e)
{
// Get
LinkButton lbtn = sender as LinkButton;
GridViewRow row = lbtn.NamingContainer as GridViewRow;
DoTheMath(row, false);
}
From code I add a TextBox to a column
protected void grdPartsBeingMonitored_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
foreach (GridColumn column in item.OwnerTableView.RenderColumns)
{
if (column.UniqueName == "MyColumn")
{
TextBox tbEditBox = new TextBox();
tbEditBox.Text = item[column].Text;
tbEditBox.Width = Unit.Pixel(50);
tbEditBox.ID = "editDemand";
item[column].Controls.Clear();
item[column].Controls.Add(tbEditBox);
}
}
}
Now how do I loop through each row and retrieve that rows value of the TextBox? Here's a start I believe:
foreach (GridDataItem item in grd1.MasterTableView.Items)
{
foreach (GridColumn column in item.OwnerTableView.RenderColumns)
{
if (column.UniqueName == "MyColumn")
{
//HOW TO RETRIEVE VALUE OF THE TEXTBOX HERE???
I tried this with no luck
foreach (Object c in item[column].Controls)
{
if (c.GetType() == typeof(TextBox))
{
TextBox tbEditBox = (TextBox)c;
System.Diagnostics.Debug.Write(tbEditBox.Text);
}
}
Please try with the below code snippet.
ASPX
<div>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="Name" DataField="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Show edit" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Get edit value" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="hide edit" OnClick="Button3_Click" />
</div>
ASPX.CS
public bool IsEditable
{
get
{
if (ViewState["IsEdit"] == null)
return false;
else
return (bool)ViewState["IsEdit"];
}
set
{
ViewState["IsEdit"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "Name1");
dt.Rows.Add(2, "Name2");
dt.Rows.Add(3, "Name3");
RadGrid1.DataSource = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
IsEditable = true;
RadGrid1.Rebind();
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Response.Write((item.FindControl("txtName") as TextBox).Text);
//perform your DB update here
}
}
protected void Button3_Click(object sender, EventArgs e)
{
IsEditable = false;
RadGrid1.Rebind();
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
Label lblName = item.FindControl("lblName") as Label;
TextBox txtName = item.FindControl("txtName") as TextBox;
lblName.Visible = !IsEditable;
txtName.Visible = IsEditable;
}
}
Let me know if any concern.
I have this manually update code for my radgrid (RAD13).
on upload it works but the thing is that only the first row in the grid saves it's update.
I think that I should passe a value that will autoincrement to passe through rows
protected void UpdateButton_Click(object sender, EventArgs e)
{
RadGrid grid = (this.FindControl("RAD13") as RadGrid);
(grid.MasterTableView.GetItems(GridItemType.EditItem)[0] as GridEditableItem).FireCommandEvent(RadGrid.UpdateCommandName, string.Empty);
}
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnUpdateCommand="RadGrid1_UpdateCommand" AllowFilteringByColumn="true" AllowPaging="true"
AllowMultiRowEdit="true">
<MasterTableView DataKeyNames="ID" EditMode="InPlace">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="update All edit row" />
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name = "Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"},
new { ID = 4, Name = "Name4"},
new { ID = 5, Name = "Name5"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem item = e.Item as GridEditableItem;
UpdateLogic(item);
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridEditableItem item in RadGrid1.EditItems)
{
UpdateLogic(item);
item.Edit = false;
}
RadGrid1.Rebind();
}
protected void UpdateLogic(GridEditableItem item)
{
// perform your update logic here
}
Let me know if any concern.
i'm a newby at .NET and I'm having a problem with my checkboxes. They all return false, even if they are selected. Here is my asp code
<asp:GridView ID="gvGeneros1" runat="server" class="divTable"
AutoGenerateColumns="False" DataKeyNames="idgenero" CssClass="table">
<Columns>
<asp:BoundField DataField="nome" HeaderText="GĂȘnero" SortExpression="nome" >
<ControlStyle Width="200px" />
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:TemplateField AccessibleHeaderText="Check">
<ItemTemplate>
<asp:CheckBox ID="checkGenero" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and here's my c# code
DataSet dsDivided;
protected void Page_Load(object sender, EventArgs e)
{
Music musicbll = new Music();
DataSet dsGeneros = musicbll.getGenders();
int size = dsGeneros.Tables[0].Rows.Count;
dsDivided = null;
// Divide in two DataTable
dsDivided = Tools.SplitDataTableInTwo((DataTable)dsGeneros.Tables[0], size / 2);
gvGeneros1.DataSource = dsDivided.Tables["FirstSet"];
gvGeneros1.DataBind();
for (int i = 0; i < gvGeneros1.Rows.Count; i++)
{
((CheckBox)gvGeneros1.Rows[i].Cells[1].Controls[1]).Checked=false;
}
}
protected void btGravarPrefs_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("generos");
dt.Columns.Add("idgenero", typeof(int));
dt.Columns.Add("active", typeof(bool));
for (int i = 0; i < gvGeneros1.Rows.Count; i++)
{
int idCliente = (int)dsDivided.Tables[0].Rows[i][0];
bool check = ((CheckBox)gvGeneros1.Rows[i].Cells[1].Controls[1]).Checked; //always false
dt.Rows.Add(new object[] { idCliente, check});
}
}
}
I don't know what to try more, and i search all over and it seems right. Thanks
I think you should wrap your Data binding with if (!Page.IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind your datasource here
}
}