I have a GridView on my webpage whose DataSource is a DataTable which is populated at runtime. AllowSorting property of GridView is True. I have successfully implemented manual sorting for this GridView.
But I had to translate the webpage to other languages for which I used local resource files. I changed the Header text of GridView columns in RowDataBound event. Since then I'm unable to sort the GridView.
protected void GVSummaryTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells.Count > 0)
{
//Translate header text
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = GetLocalResourceObject("GVSummaryTableName").ToString();
e.Row.Cells[1].Text = GetLocalResourceObject("GVSummaryTableLoginLevel").ToString();
e.Row.Cells[2].Text = GetLocalResourceObject("GVSummaryTableLoginID").ToString();
e.Row.Cells[4].Text = GetLocalResourceObject("GVSummaryTableDate").ToString();
}
}
}
What should I do to enable sorting for the columns?
Any help would be appreciated. Thanks!
Changing the code to below solved the problem:
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton LnkHeaderText = e.Row.Cells[1].Controls[0] as LinkButton;
LnkHeaderText.Text = "Name";
}
I am not sure if the problem is related to the Header text since the sorting is normally done with the Sort Expression. Please, make sure you are also giving a value to this property when doing the Sorting. Hope this helps!
Related
I have a GridView that is assosiated with database. Here's a data binding:
protected void GridViewProgramms_SelectedIndexChanged(object sender, EventArgs e)
{
int rowIndex = ((GridView) sender).SelectedIndex;
var programid = ((GridView) sender).Rows[rowIndex].Cells[1].Text;
GridViewEx.RowEditing += GridViewEx_RowEditing;
SqlDataSource1.SelectParameters["ID"].DefaultValue = programid;
GridViewEx.DataBind();
ExcersicePanel.Visible = true;
PanelAp.Visible = false;
}
Everything works fine, but I need to change some cells values in GridView after that. I need to rewrite every Cell in the last row. How to do this without affecting the database?
You will need to write your code(To change the cell text) in RowDataBound event of the gridview after data bind
Refer following link for more explanation
Update GridView Column after databinding
protected void GridViewEx_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
label or textbox ddltype = (label or textbox)e.Row.FindControl("id");
ddltype.text="ur text";
}
}
I am a new ASP.NET developer. I am using a DetailsView now to dispaly some data from the database. I have the need to highlight certain two rows from the DetailsView. Both rows are VARCHAR data type.
SO HOW TO DO THAT?
Override the databound event and set the e.Row.BackColor = System.Drawing.Color.Red; or what ever color, if you have some logic to execute for finding the row that needs to be highlighted.
protected void detailsView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(...)//some condition for selection of row to be higlighted
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
If you need a perticular row, say 4th row to be highlighted and that's fixed then you could directly like this
detailsViewGrid.Rows[3].Row.BackColor = System.Drawing.Color.Red;
The only thing you need to keep in mind that this code would be written in only those events which would come after grid_rowdatabound event(Like pre-render)
I am new in ASP.NET ,I would have editable gridview in asp.net using C# , I found this editable gridview (Database , Project) in codeproject but i didn't realize how
its work specially this part of code:`
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dp= (DropDownList )e.Row .FindControl ("DropDownList1");
DataTable dt = load_department();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[i][0].ToString();
dp.Items.Add(lt);
}
dp.SelectedValue = drv[3].ToString();
RadioButtonList rbtnl = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
rbtnl.SelectedValue = drv[5].ToString();
CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
chkb.SelectedValue = drv[6].ToString();
}
}
}
`
Why she/he do this??
RowDataBound event fires when you bind the grid to a datasource, say, for example, a datatable.
For each row in the datatable, this code will run, and will, depending on the values in that row, put a value in each column of the grid view.
FindControl is used to find the control with the spcific name on that line of the grid view - remember, it will be repeated many times for as many rows as you have.
Once the control has been found, the value is set.
You are effectively setting up each row of the grid view for each row of the data in your data source.
Take a look at http://msdn.microsoft.com/en-us/magazine/cc163933.aspx for an overview of the intent behind this control.
** RowDataBound Occurs when a data row is bound to data in a GridView control.
**DataControlRowState Specifies the state of a row in a data control for eg.Edit,Insert,Selected etc
** RowState Gets the current state of the row with regard to its relationship to the DataRowCollection.
now in that if condition your dropdownbox (DropDownList1) is filled and RadioButton and Checkebox are setting up their selected values.
I am currently working on a grid view. I am using the allow paging method which is working fine and positioned to the right of the grid view.
I want to hide the first column which is working fine except it also removing the paging numbers which stops the user from being able to change page numbers.
Below is the code I am using to hide the column
protected void tblLog_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
The above code hides the correct column but also hides the automatic page numbers created by the grid view allowpaging method.
Thanks for any help you can provide.
Check first to see if it's actually a data row:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Visible = false;
}
You need to add a condition for datarow and dataheader both
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}
I have a gridview control which contains an image field. However, the image url is built from several fields in my returned dataset. How do you concatenate those fields together, and at which point do I do this so that I can pass that image url do my imagefield in my gridview? I am guessing it's on the RowDataBound but I don't know how to access each of the rows in my dataset?
Thanks.
I'm not sure what you are trying to concatenate without a code example, however I would perform the concatenation before binding the gridview and store it in a private member on the class so that you can access it later in the RowDataBound event.
You can use the row data bound event to find the control within that row and set its ImageUrl property.
private string m_ConcatUrl;
protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs args)
{
if(args.Row.RowType == DataControlRowType.DataRow)
{
Image imgCtrl = (Image) args.Row.FindControl("imgCtrl");
imgCtrl.ImageUrl = m_ConcatUrl;
}
}
You can use RowDataBound event to set the cell value.
void YourGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// You can set your cell value in this
e.Row.Cells[index] // use this to set the value in the cell
}
}
In .NET 4, it would be like this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
string1 = e.Row.Cells[i].Text;
}
i is the index of the particular column you want to reference (e.g. Cells[0] is column 1).
I'm not sure if it's different in 3.5 but try that.