how to bind data in footer of gridview - c#

I have a gridview and I have to bind data in gridview as well as the footer of the gridview. I have tried many things but nothing is working. Can anyone tell me what I have to do can? I bind the gridview in the footer with the same query or I have to write diffrent query for gridview and footer. Below is my work. What I have tried I write diffrent query for both gridview and footer
System.Web.UI.WebControls.GridView grdview = ystem.Web.UI.WebControls.GridView)sender);
Label lblenqid = (Label)e.Row.FindControl("lblenqid1");
Label lblactualcost = (Label)grdview.FooterRow.FindControl("Label44") as Label;
Label lblpackagecost = (Label)grdview.FooterRow.FindControl("Label42") as Label;
Label lbldriver = (Label)grdview.FooterRow.FindControl("Label46") as Label;
objHotel.Tour_DetailVehiclePackageCost(lblenqid.Text,
blactualcost,lblpackagecost,lbldriver);
plz help me thanks in advance

protected void yourNiceGridViewControl_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox myTextBox = e.Row.FindControl("txtFooter") as TextBox;
if( myTextBox != null )
{
myTextBox.Tex= ds.Tables[0].Rows[0]["MyFirend"].ToString();
}
}
}
Try this.

Related

onRowdatabound not giving label value in asp.net

I am creating the dynamic grid and adding textbox and label in row dynamically on some button click and bind some data from the database to textbox and label.I want to change the label text color and text box read only for condition depend on database.i have used onRowDataBound event of gridview but not getting any value in the textbox and label for a row. Can anyone helps me to solve this issue? Thanks
protected void grdMasterData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label status = e.Row.FindControl("lblProduct") as Label;
if (status.Text == "LY Actuals")
{
e.Row.Cells[0].CssClass = "lblProductColor";
// lbtAction.Visible = false;
}
}
}
In RowDataBound three row types provided by grid, you are using only DataRow, try to get your values from empty row and footer row because first record will not come in DataRow.

converting Gridview Code to Listview Code

i'm trying to convert a gridview into a listview. The bit that is troubling me is that i can't get at following codes
TextBox box1 = (TextBox)Gridview1.Rows(rowIndex).Cells(1).FindControl("clientCode");
Please help me to achieve this code in Listview Format.
(TextBox)listview1.Items(rowIndex).FindControl("txtFName")
This is what you want
You can use ItemCommand event of listview to get the text box control which used in List View control
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
TextBox txtFname = (TextBox)e.Item.FindControl("txtFname");
}
}
Is this what you want?
TextBox box1 = (TextBox)ListView1.FindControl("clientCode")
get text
box1.Text = ListView1.SelectedItems[rowIndex].SubItems[1].Text;

How to get readonly textbox value in gridview c# from code behind

I have a grid and I need to get readonly textbox value in gridview c# from code behind
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (txt.Text != "")
{
txt.Attributes.Add("readonly", "readonly");
txt.Visible = false;
lbltaskname.Visible = true;
}
else
{
txt.Attributes.Remove("readonly");
}
}
}
You can do itself on aspx page.
Assuming that dataset is attached(bind) to the grid from code behind, now coming to gridview on aspx page , add textbox control to grid , in text property of texbox assign value from dataset by -
<%# Bind("your_column_name")%>
and add
readonly = true
to textbox.
Thanks

trying to find control which is inside gridview which happens to be inside a repeater.

So I have a repeater which has a gridview in it. I need to find a hyperlink field that is inside the gridview. I am able to find the gridview using the following code, but then when I try to find the hyperlink inside that gridview, my program crashes.
protected void CompletedRepeater_DataBound(object sender, RepeaterItemEventArgs e)
{
Repeater rpt = (Repeater) sender;
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
GridView gv = (GridView)e.Item.FindControl("CompletedGridr");
if (gv != null)
{
}
}
}
With the above code I am able to find the gridview.
I want to find the hyperlink inside the
if (gv != null)
{
block.
Any ideas on how I can achieve this?
Do something like this:
foreach(GridViewRow row in gv.Rows)
{
HtmlGenericControl linkTag= row.FindControl("linktag") as HtmlGenericControl;
}
or you can do like this if you are using <asp:HyperLink> :
HyperLink myHyperLink = row.FindControl("myHyperLinkID") as HyperLink;

GridView column with checkbox field with SQL

I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}

Categories

Resources