how to update datatable hyperlink - c#

I have a datatable and it contains a website url and I am displaying entire data in gridview, I want to add hyperlink to all existing urls before binding to gridview.
I am getting data dynamically from database , So I use autogenenerate= true
is it possible ?

You can listen to the OnRowDataBound event and from there trying to infer which cells contain a URL and then, turn them into a link:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell cell in e.Row.Cells)
{
if (cell.Text.StartsWith("http"))
{
cell.Text = $"<a href='{cell.Text}'>{cell.Text}</a>";
}
}
}
}

Related

how to change the value of a gridview in row data bound event

i want to hide last 4 digits of mobile number in gridview and show last 4 digits as **** . im getting only header value not item template values. how to get mobile values/item values and edit it and assign to grid view?
protected void gvrequests_RowDataBound(object sender, GridViewRowEventArgs e)
{
string Mobile = e.Row.Cells[3].Text;
string securedPhone = Mobile .Remove(6);
string MobileSecured= securedPhone + "****";
e.Row.Cells[3].Text=MobileSecured
}
You need to check the row first that it is DataRow or not like this.
protected void gvrequests_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// your logic will go here
}
}
protected void gvrequests_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color: #87CEFA";
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Mobile = e.Row.Cells[3].Text;
string securedPhone = Mobile .Remove(6);
string MobileSecured= securedPhone + "****";
e.Row.Cells[3].Text = MobileSecured;
}
}
RowDatabBound even fired on each row means for header row , data row (alternate row too) and footer row.
So, when you want to manipulate the data, as #Sain suggest, you check that it is datarow then our logic implement.
The same logic is also apply for header and footer, but ideally we should use for the data row only.

changing data type of autogenerated column

I have a Gridview that auto generates columns...
The problem is, the data type are all imageurl.
when the gridview populates, the url is in label format.
I could define them as templatefield which works, but the requirement is to auto generate.
I read on MSDN of this, but i dont know how to proceed from there
private void BUChecker_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
}
You can alter this on its TemplateFields. See GridView controls'.
see this for your reference.
I think you use RowDataBound as follows In case you don't want to use template field
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find your cell suppose it is Cell 0
string imgUrl=e.Row.Cells[0].Text;
Image img = new Imge();
img.ImageUrl =imgUrl;
e.Row.Cells[0].Controls.Add(img);
}
}

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);
}
}

c# GridView RowDataBound Error

I have a gridview that should be editable when a row is clicked. This gridview is clickable in a row so when I clicked it, the row will be displayed in other pages for editing reason. I got an error like this
Specified argument was out of the range of valid values.
Parameter name: index.
This is happening for this line:
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
How can I fix this?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get reference to button field in the gridview.
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
e.Row.Attributes["onclick"] = _jsSingle;
}
}
}
You should be using something like e.Row.FindControl("linkbuttonid"). This will get you the required link button from the current row, then you can attach your handlers to the same and perform your logic

Accessing Gridview columns by Row.Cells

Hai I am using the following code to access columns in a row but always displays empty string when using Row.Cells[1].Text I am using this code in GridView Unload event handler.
Thanks inn advance.
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
I would suggest using the Gridview Databound event instead. Because the
Unload event occurs when the server control is unloaded from memory.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
}
Databound events occur after the server control binds to a data source.
To understand how gridview events work, look at MSDN
It could be done in Gridviews RowDataBound Event ie
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
String coltext = e.Row.Cells[1].Text
}
else if(e.Row.RowType == DataControlRowType.DataRow)
{
String coltext = e.Row.Cells[1].Text
}
}
Hope this helps.

Categories

Resources