I am using a gridview in aspx page. I am adding Textboxes dynamically to the griview header. How can I insert a value to the textbox in the grid header.
My code goes like
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TextBox txtBox1 = default(TextBox);
for (int i = 0; i <= (e.Row.Cells.Count - 1); i++)
{
litHeader.Text = e.Row.Cells[i].Text + "<br/>";
e.Row.Cells[i].Controls.Add(litHeader);
e.Row.Cells[i].Controls.Add(txtBox1);
}
}
}
I am trying to bind a value to the textbox 'txtBox1' using the code
private void FillGridView()
{
TextBox txt;
txt = (TextBox)GridView1.HeaderRow.FindControl("txtBox1");
txt.Text = dtValues.Rows[0][5].ToString();
}
But I am not able to fill the textbox. Please help.
This may help someone on very old question. This how I update my header row text box immediately after I insert data to GV1. Hope it helps.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
if (e.Row.RowType == DataControlRowType.Header)
{
//((TextBox)GridView1.HeaderRow.FindControl("tbProjNumInsert")).Text
if (newMaxNum != null && newMaxNum.Length > 0)
{
((TextBox)e.Row.Cells[4].FindControl("tbProjNumInsert")).Text = newMaxNum;
System.Diagnostics.Debug.WriteLine("GridView1_RowDataBound()...GridView1.Width !!!! " + ((TextBox)e.Row.Cells[4].FindControl("tbProjNumInsert")).Text + "........newMaxNum:" + newMaxNum);
}
}
}
Related
I have a grid where columns are populated dynamically. I have a column called ID which will have hyperlink enabled and hyperlinks needs to be disabled if cells value is null or empty.
For ex:
If a cell value returns 0 or null values for that ID column then I need to disable the hyperlink for those cells in that columns. The below code adds hyperlink successfully and top of it I need to check for null or 0 values in the cell.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string strHeaderRow = GridView1.HeaderRow.Cells[i].Text;
if (strHeaderRow == "ID")
{
string strMktURL = "http://www.address.com";
HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);
}
}
}
}
protected HyperLink AddHyperLink(TableCell cell, string strURL)
{
HyperLink hl = new HyperLink();
hl.Text = cell.Text;
hl.Font.Underline = true;
hl.Target = "_blank";
hl.NavigateUrl = strURL;
hl.Attributes.Add("style", "color:Black;");
cell.Controls.Add(hl);
return hl;
}
Please suggest how this can be achieved.
Just check for your cell value is null or empty and don't make it hyperlink.
Also I will suggest to make your AddHyperLink() void, if you will not use its returned value.
UPDATE
TableCell stores its value into string Text property, so:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strMktURL = "http://www.address.com";
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string strHeaderRow = GridView1.HeaderRow.Cells[i].Text;
if (strHeaderRow == "ID")
{
if(!string.IsNullOrEmpty(e.Row.Cells[i].Text))
{
HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);
}
}
}
}
}
i have a gridview in which i have created dynamic rows in row created event as shown in below code.
protected void grdPBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
strPreviousRowID = DataBinder.Eval(e.Row.Date).ToString()}
grdPBook.ShowFooter = false;
}
}
protected void grdPBook_RowCreated(object sender, GridViewRowEventArgs e)
{
bool IsSubTotalRowNeedToAdd = false;
bool IsGrandTotalRowNeedtoAdd = false;
if (ddlSummary.SelectedValue == "0")
{
if ((strPreviousRowID != string.Empty)(DataBinder.Eval(e.Row.DataItem,Date) != null))
{
if (strPreviousRowID != DataBinder.Eval(e.Row.DataItem, Date).ToString())
{
if (ddlSummary.SelectedValue == "0")
{
IsSubTotalRowNeedToAdd = true;
}
}
}
if (IsSubTotalRowNeedToAdd)
{
// ---code for adding dynamic subtotal row-----
}
}
}
when i print the grid the print dialog opens and after closing the dialog the dynamically generated columns of grid disappers and the grid gets messed up coz i m not able to retain the values for Date(here)on the basis of which the dynamic rowsare generated.
How can i achieve the task.Help me.
protected void grdPBook_RowCreated(object sender, GridViewRowEventArgs e)
{
// your code
if (ddlSummary.SelectedValue == "0")
{
//your code
grdPBook.DataSource = dt;
//here dt is your Data Table object containing all rows.
}
grdPBook.DataBind();
}
Use grdPBook.DataBind(); in every Row operations.
I have a grid view ,i use the row command to open a specific window with several parameters.
Now i want to remove this button and make the whole row clickable so if i click on the row open the same window .How to do that .
protected void gv_Details1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Confirm")
{
int index = Convert.ToInt32(e.CommandArgument);
Session["task_code"] = ((HiddenField)gv_Details1.Rows[index].Cells[0].FindControl("hf_tc")).Value;
Session["trans_serial"] = ((HiddenField)gv_Details1.Rows[index].Cells[0].FindControl("hf_ts")).Value;
MasterPage2 obj = (MasterPage2)Page.Master;
obj.SetMainVariables();
obj.PreValidate();
obj.SetDepartment();
Response.Redirect("~/Contents/" + Session["page_new"].ToString() + ".aspx", false);
}
}
Add RowDatabound event :
protected void gv_Details1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("style", "cursor:pointer;");
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='pagename.aspx?Rumid=" + abc + "'";
}
}
Thanks :
Curtsy : http://forums.asp.net/t/1859787.aspx/1?How+to+fire+RowCommand+event+when+user+click+anywhere+in+the+gridview+row
two of the best links of our own stack overflow community.
How to implement full row selecting in GridView without select button?
Making an entire row clickable in a gridview
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Product p = (e.Row.DataItem as Product);
e.Row.Attributes.Add("onClick", "location.href='somepage.aspx?id=" + p.ProductID + "'");
}
}
string search= textbox1.text;
protected void grd_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell tc in e.Row.Cells)
{
tc.Text = tc.Text.Replace(search, "<span style='color:Red;'>" + search + "</span>");
}
}
}
I'm using that code to highlight a word searched but when I Debug in browser buttons:edit,select,delete are not there .and if i deleted the event the buttons are back.
what shall I do?
One way to do this would be to check and see if the cell contains controls... It looks like the rest of your grid view cells just contain text and you are appending the span into the cell to highlight the search value.
string search= textbox1.text;
protected void grd_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell tc in e.Row.Cells)
{
if (tc.Controls.Count == 0){
tc.Text = tc.Text.Replace(search, "<span style='color:Red;'>" + search + "</span>");
}
}
}
}
This will bypass the cells that contain the select, edit and delete controls. And also text boxes and labels and things like that as well. Hope this helps.
I am using a dropdownlist in footer row of gridview(ASP.Net) and I fill that on rowdatabound event,first time it works fine but when the form is postbacked,dropdown gets cleared.
It can be solved by refilling it on each postback,but I want that only single time code binding call fulfill my need,
means is there any way to stop from being null on postback.
looking for your kind solutions and suggestions
Thnx in advance......
Supriya
Code:
protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (gvProjects.Rows.Count > 0 && e.Row.RowIndex != -1)
{
string proj_Id = Convert.ToString(gvProjects.DataKeys[e.Row.RowIndex].Value);
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlProject = (DropDownList)e.Row.FindControl("ddlProject");
if (ddlProject != null && ddlProject.Items.Count == 0)
{
objMaster.FillProjects(ddlProject);
ddlProject.SelectedValue = proj_Id;
}
}
}
}
catch (Exception excep)
{
lbl_msg.Text = excep.Message;
}
}
It's called whenever the grid is binded,can it be avoided.
With this code you will avoid filling the dropdownlist located in the gridview footer in each rowdatabound:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Footer)
{
//do the binding for the normal rows
}
}
As you can see, the rowdatabound will be only executed on the header and normal rows but not in the footer.
Hope this helps!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropdown();
}
}