I would like to get the column (Description) from DataRowView.I have the following code.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
DataRowView dataRowView = ???????????
string description = dataRowView["Description"] as string;
}
}
How to replace ?????????? with appropriate code?
Use (DataRowView)e.Row.DataItem;
Related
How do I set the GridView Category Columns Titles manually if I'm databinding manually?
namespace Workforce
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var s = Work.DataLayer.Connection("test");
var x = Work.DataLayer.GetCourseList(s);
GridView1.DataSource = x;
GridView1.DataBind();
}
}
}
In the design view, there is a data source id which i'm not using.
How about this
GridView1.HeaderRow.Cells[0].Text = "New Header";
Like #Rahul stated, you want to do this in the RowDataBound event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Your Better Column Header";
}
}
And repeat for each row header you want to modify, ensuring you have used the correct ordinal number.
Is this possible?
I have a table with user accounts retrieved from a database. Then at the start of each column I would like to add a checkbox which if selected will select the account. I tried experimenting with it and I can't seem to put the checkbox inside the table. How can I do this?
You can try this:
const int ColumnSelect = 0;
protected void Page_Load(object sender, EventArgs e)
{
//Get real data here.
DataTable dt = new DataTable();
dt.Columns.Add("count");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "5";
GridView1.Columns.Add(new TemplateField());
BoundField b = new BoundField();
GridView1.Columns.Add(b);
b.DataField = "count";
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
//Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
if (cb.Checked)
{
//Do something here.
}
}
}
I'm implementing full text search on a table named as tbljobs on the cloumn named as jobdescription. In front end I'm getting html tags in the description. I'm showing the records in GridView and on Gridview's RowDataBound enent I'm decoding the text. I'm using the following code on Gridview's RowDataBound event:
protected void GridNewlyPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(((Label)e.Row.FindControl("lblJobDescription")).Text);
((Label)e.Row.FindControl("lblJobDescription")).Text = decodedText;
}
}
But nothing works..!!
Use DataRowView to get the Data...
protected void GridNewlyPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(rowView["jobdescription"]);
((Label)e.Row.FindControl("lblJobDescription")).Text = decodedText;
}
}
How can I get the value of a gridview cell? I have been trying the code below with no luck.
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e) {
int test = Convert.toInt32(e.Row.Cells[5].text;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.DataRow)
{
string LinkText = (string)System.Web.UI.DataBinder.Eval(e.Row.DataItem, "RegistrationLinkText");
if(LinkText == "text")
{
e.Row.Cells[3].Text = "your text";
}
}
}
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int test = Convert.toInt32(e.Row.Cells[5].Text);
}
}
use this code below,
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int test = Convert.ToInt32(Server.HtmlDecode(e.Row.Cells[5].Text.Trim()));
}
}
and please remember cell index number starts from 0
If you use the above methods you will get the value of cell[5] for the last row only.
So if you are specific about a certain row I think you can get the value from any other gridview event handlers.
1)Is it possible to arrane strings to the right?
2)Is it possible in case The value of the field is null to have "-" in the corresonding gridview field
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Columns[2].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
}
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Due to the fact that DBnull value by default would be just empty string
e.Row.Cells[1].Text = (string.IsNullOrEmpty(e.Row.Cells[1].Text))?"-":e.Row.Cells[1].Text;
}
}