Set data to a label in GridView - asp.net

I have a label in an asp GridView and I want to set bind data to it.
Here is my RowDataBound method:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
var name = (Label)e.Row.FindControl("Label_name");
name.Text ="sara";
name.DataBind();
}
and here is my GridView:
<asp:GridView ID="GridViewCertificateType" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" >
<asp:Label ID="Label_name" runat="server" Text="" ></asp:Label>
But after name.Text ="sara";, I receive this exception:
Object reference not set to an instance of an object.

In your RowDataBound method, make sure you have a condition to check what the type of the row is. This event will be called for every row in your GridView, including header and footer rows. If Label_name is not found in the header row, you'll have a null object. Once you do that, get rid of name.DataBind();, as it is not needed.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var name = (Label)e.Row.FindControl("Label_name");
name.Text = "sara";
}
}

try this
var name= (Label)e.Row.Cells[X].FindControl("Label_name");
name.Text ="sara";
name.DataBind();
X means index of column

Related

delete existing row from data table

I need to delete a row from datatable according to the selected value from the grid view
I tried to make it, but I failed
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var row = GV_Repair.Rows[e.RowIndex];
newdt.Rows.Remove();
bind_grid();
}
If you have a static data source, you can do this:
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
newdt.Rows.RemoveAt(e.RowIndex);
bind_grid();
}
If the datasource is dynamic, you will need a hidden field (if you don't have one already) in GridView's template field:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnId" runat="server" Value='<%# Eval("ID") %>' />
<!-- Other Items -->
</ItemTemplate>
</asp:TemplateField>
And change the row deleting method:
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = 0;
GridViewRow row = GV_Repair.Rows[e.RowIndex];
HiddenField hdnId = row.FindControls("hdnId") as HiddenField;
if(hdnId != null && int.TryParse(hdnId.Value, out id)
{
//Logic to delete from table where Id = id
}
bind_grid();
}

How to update an editItemTemplate checkboxlist from codebehind?

I am using a gridview fed from a SQL database. I need to add a custom itemTemplate that will consist of a checkboxlist fed from another datasource. Here is my xml part :
<asp:TemplateField HeaderText="Equipements" >
<EditItemTemplate>
<asp:CheckBoxList ID="myCB" runat="server">
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
I am using the gridview onEditing event to try to access "myCB" in the code behind :
protected void OnEditing(object sender, EventArgs e)
{
GridView gridview = sender as GridView;
GridViewEditEventArgs editEvent = e as GridViewEditEventArgs;
ListViewItemEventArgs rowEvent = e as ListViewItemEventArgs;
TableCell equipementsCell = gridview.Rows[editEvent.NewEditIndex].Cells[11];
CheckBoxList equipements = gridview.Rows.FindControl("myCB") as CheckBoxList;
}
This code doesn't work, the checkboxlist isn't found. I have tried many things unsuccessfully...
You should use GridView's RowDataBound event:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow
&& e.Row.RowState == DataControlRowState.Edit)
{
CheckBoxList equipements = (CheckBoxList )e.Row.FindControl("myCB");
equipements.DataSource = getSomeData();
equipements.DataTextField = "TextColumn";
equipements.DataTextField = "IdColumn";
equipements.DataBind();
}
}

Gridview Row - value and column name

I would like to get name of column which I click and value of this clicked row.
I found very good example but I`m not able to get this data which I want.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id_lekarza=" + DataBinder.Eval(e.Row.DataItem, "Id_lekarza") + " &klikniete=" + "sss" + " '";
}
}
e.Row.RowIndex all the time 0?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:alert('" + e.Row.RowIndex + "');");
e.Row.Style.Add("cursor", "pointer");
}
}
In your GridView you can add a template field like below :
<asp:TemplateField ItemStyle-Width="44px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
HeaderStyle-Height="40px" ShowHeader="false">
<ItemTemplate>
<asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png"
CommandName="Select" CommandArgument='<%# Eval("ID") %>'/></ItemTemplate>
<HeaderStyle Height="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="44px"></ItemStyle>
</asp:TemplateField>
And in your C# codebehind You should create the RowCommand event of the Gridview , and access the CommandArgument of that row like following:
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
//You get the ID of that specific row that you have selected :
string ID=e.CommandArgument.ToString();
//Here you write your code ! For example a select query to get that row from database by the ID you have gained in above line of code
}
}

How to make visible a column of gridview?

I am using Gridview.In this i have 2 columns i.e. department and emailID.In this gridview department is bind from database and showed in linkbutton in gridview.
I want to do that when i clicked on thst dept linkbutton it make visible the column emailID.
How can i do this?Plaese guide me..
Thanks in advance.
Here is My Grid:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDept" runat="server" Text='<%#Bind("Department")%>' OnClick="lnkbtnTitle_Click" ></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="50%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="pnlN24" runat="server" Visible="false">
<asp:Label ID="lblTotal" runat="server" Width="30" Text="abc"></asp:Label>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to handle the events of GridView control especially RowCommand.
public class Demo
{
public string Dept { get; set; }
public string Email { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Demo> list = new List<Demo>()
{
new Demo() { Dept="A", Email="a#a.com" },
new Demo() { Dept="B", Email="b#b.com" },
};
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmd")
{
GridViewRow row = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;
Label email = row.Cells[1].FindControl("email") as Label;
email.Visible = true;
}
}
Understanding that you will make visible all the cells in the column for all the rows, you can handle the OnClick event of the dept button. For example.
<asp:LinkButton ID="lnkDept" OnClick="LinkButton_Click" runat="server" Text="Department" />
Code behind:
protected void LinkButton_Click(Object sender, EventArgs e)
{
gridView1.Columns[1].Visible=true;
}
Where gridView.Column[1] is your email column.
its not possible to make cell visible when column's visibility property set to false. so for showing only adjacent cell visible .
you can use the only one column of type template field and then make a table in that template field and set the td of email to visible false and on row command of grid as the previous answers making that lable visible you should make your td visible .
protected void lnkbtnTitle_Click(object sender, EventArgs e)
{
GridViewRow gvrow = ((LinkButton)sender).Parent.Parent as GridViewRow;
Panel pnlN24 = (Panel)gvrow.FindControl("pnlN24");
pnlN24.Visible = true;
}
It is very simple , the above code will work.
We have to find in which row the LinkButton is Clicked , which we can get from the
the following code. Once you get the GridViewRow then find the control in that row and make it Visible
GridViewRow gvrow = ((LinkButton)sender).Parent.Parent as GridViewRow;
As we know that
Control <--- Cell <-- GridViewRow <-- GridView
For Eg:
LinkButton <--- Cell <-- GridViewRow <-- GridView
GridViewRow is Parent to Cell and Cell is Parent to the Control inside the Cell (Panel)
function validateColors(id) {
var grid = document.getElementById("GridView1");
var label = grid.rows[id].cells[6].children[2];
grid.rows[id].cells[6].children[2].style.visibility = "visible";
grid.rows[id].cells[6].children[2].style.visibility = "hidden";
}

Why is the footer-item not included in Repeater.Items?

I need to get a value from a textbox inside a FooterTemplate in the OnClick event of a button. My first thought was to loop through the items-property on my repeater, but as you can see in this sample, it only includes the actual databound items, not the footer-item.
ASPX:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
Item<br />
</ItemTemplate>
<FooterTemplate>
Footer<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Code-behind.cs:
protected void Page_Load(object sender, EventArgs e)
{
ListItemCollection items = new ListItemCollection();
items.Add("value1");
items.Add("value2");
Repeater1.DataSource = items;
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(Repeater1.Items.Count);
}
This code will only output "2" as the count, so how do I get to reference my textbox inside the footertemplate?
From the MSDN documentation, the Items is simply a set of RepeaterItems based off the DataSource that you are binding to and does not include items in the Header or FooterTemplates.
If you want to reference the textbox, you can get a reference on ItemDataBound event from the repeater where you can test for the footer.
E.g.
private void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox textBox = e.Item.FindControl("TextBox1") as TextBox;
}
}
You can find controls in the repeater. That will give you all the controls in the repeater (RepeaterItems collection). Now you can do something like this:
RepeaterItem footerItem=null;
foreach(Control cnt in Repeater1.Controls)
{
if(cnt.GetType() == typeof(RepeaterItem) && ((RepeaterItem)cnt).ItemType == ListItemType.Footer)
{
footerItem = cnt;
break;
}
}
The footer should be the last child control of the repeater so you can do something like..
RepeaterItem riFooter = Repeater1.Controls[Repeater1.Controls.Count - 1] as RepeaterItem;
if (riFooter != null && riFooter.ItemType == ListItemType.Footer) {
TextBox TextBox1 = riFooter.FindControl("TextBox1") as TextBox;
if (TextBox1 != null) {
TextBox1.Text = "Test";
}
}

Resources