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);
}
}
}
}
}
Related
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 added gridview that is binds with csv file data that stored in session variable like this:
dgData.DataSource = Session["csvdata"];
dgData.DataBind();
It binds perfectly.But i need to add dropdownlist at first row of gridview for mapping of database column name with grid header.I used this code to add dropdownlist.But dropdown added to header instead of first row.
protected void dgData_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header )
{
for (Int32 i = 0; i < e.Row.Cells.Count; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddlCol" + i.ToString ();
e.Row.Cells[i].Controls.Add(ddl);
}
}
}
protected void dgData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl;
if (e.Row.RowIndex == 0)
{
for (Int32 i = 0; i < e.Row.Cells.Count; i++)
{
ddl = new DropDownList();
ddl.ID = "ddlCol" + i.ToString ();
e.Row.Cells[i].Controls.Add(ddl);
}
}
}
}
this code add's control to exatly first row of grid-view
I have a label control on my page and beneath the label control there is a Gridview.The value that exists in the label control also exists in the Gridview. The label control has value like this
3244|Yellow Ink| Test Link
In the gridview, I have a value 3244 too
3244 yello Ink Test Link
3255 Green Link Test2
I want the row Index of 3244 in my code behind as soon as the page loads. Is their any way to do it.
Not knowing what type of data source you have, one way of doing it would be with LINQ like this:
protected void Page_Load(object sender, EventArgs e)
{
// Fetch the text from your label. (I'm assuming that you have only one label with the text "3244|Yellow Ink| Test Link".
string text = Label1.Text;
// Find the first row or return null if not found.
var resultRow = GridView1.Rows.Cast<GridViewRow>().FirstOrDefault(row =>
{
// Get the id (that I'm guessing is the first (0-index) column/cell)
var id = row.Cells[0].Text;
// Return true/false if the label text starts with the same id.
return text.StartsWith(id);
});
if (resultRow != null)
{
var index = resultRow.RowIndex;
}
}
A shorter version would look like this:
var resultRow = GridView1.Rows.Cast<GridViewRow>()
.FirstOrDefault(r => text.StartsWith(r.Cells[0].Text));
protected void btnJobAppSelected_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridViewJobApplications.Rows)
{
int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
LinkButton btnJobAppSelected = (LinkButton)GridViewJobApplications.Rows[rowIndex].FindControl("btnJobAppSelected");
}
}
In your page load after populating gridView, you can do that like this:
String searchValue = "3244 ";
int rowIndex = -1;
foreach (GridViewRow row in GridViewID.Rows)
{
if (row.Cells[0].Text.ToString().Equals(searchValue))
{
rowIndex = row.RowIndex;
break;
}
}
int YourIndex = rowIndex;
I have a method called BindItems() and this method gets the values to a DataTable and then a GridView is bound to it.
Then I have an itemtemplate in that gridview which contains a drop down, this drop down gets populated from 1 -14 from code behind under Gridview_RowDataBound, now I need to figure a way to get the Quantity value in the DataTable on the other function "BindItems()" and for each Row in the gridview so a SelectedValue = datatable["Quantity"] or something.. how can I do this?
protected void BinItems(int myId)
{
//this data table contains a value "Quantity"
DataTable dt = MyClass.getItems(myId);
uxGrid.DataSource = dt;
uxGrid.DataBind();
}
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//Some how i need to do a SelectedValue = datatable where field = Quantity for each row
}
You need to access the current-being-bound-row's DataItem, which in your case, is a DataRowView (since you are binding to a DataTable):
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//here goes the "magic"
DataRowView dRow = e.Row.DataItem as DataRowView;
if(dRow != null)
{
Myddl.SelectedValue = dRow["Quantity"].ToString();
}
}
you should outside the DataTable dt and then youll be able to access it in the Function
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);
}
}
}