I know this type of question is asked before but no one got the answer yet...!!
How to get a Grid View Row from Data Keys.I don't want to iterate through the whole gird view.
I want to access specific text box(s) in a grid view.
for example in a 100 rows grid view i only want to disable any 2 text boxes on Page Load.
I have Data Key Names defined in grid, but how to get rows from it ?
any idea?
Please try following code..
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Get data row view
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find textbox control
TextBox txtname = (TextBox)e.Row.FindControl("txtName");
string Name = txtname.Text;
if (((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() == "Leave")
{
txtname.disable=true;
}
else
{
txtname.disable = false;
}
}
}
Related
I am working on a gridview in ASP.Net. I want a particular row to be highlighted on checkbox check so that if any other user logs in the system the row should appear highlighted to them too.
I would suggest create a flag field in your Database table that you bind with the grid view. If you check one row then pass true value to the flag field. Then while you show the Grid view validate like this,
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IsChecked")) == "True")
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Blue;
}
}
}
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 have a sqldatasource and gridview.
I have two columns:
ID | Status
1 Closed
2 Opened
3 Waiting
How to change the color of the label in the view state of the gridview depeding on the value from the 'status' column.
For example if the value in the cell is "Closed" the label color will be red , if it's opened then it will become green and so on.
I've thought about looping through all cells of the status column and if the cell contains a certain value the color will change. ( in row data bound event ) . But I haven't done this because I don't find this idea as a good one because of the looping part.
use the RowDataBound event from the grid view to check what is the status. You don't need to do a loop because that event (if you register or not) is being called.
One thing to note, you will need to make sure you are looking at the right row (header, footer, alternate etc) so something like this
void YourGridViewName_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Do your color change here by accessing the col with e.Row.Cells[column_index].BackColor = 'what ever you want'
}
}
You can enable the RowDataBound Event in the markup
<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">
</asp:GridView>
And put this in your Code-Behind file.
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["state"].ToString();
//format color of the as below
if(state == "Closed")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Red;
if(state == "Open")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Green;
if(state == "Waiting")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Yellow;
}
}
You have to write code in the rowdatabound event of your grid view.
Example:
private GridView1_RowDatabound(object sender,EventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// You have to put your logic here.
if( e.Row.Cells[1].Text == "closed" )
{
// to get a reference to label control
Label lb = e.Row.FindControl("LabelCOntrolID");
}
}
}
Note: i need to change in different columns based upon search request.
Yes you can do it in GridView.RowDataBound event
Like this
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = Color.Beige;
// 0 could be any valid cell index in your row
}
}
for more info Go here
how to skip specific item in row bind event C#
You could
Filter the data via the SQL query.
Filter the data via a table select if binding to a datatable/dataset
Filter data via a DataView
Use the RowDataBound event
Edit ~ Here is how to use the RowDataBound
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow) {
//A value to check for
string myValue = DataBinder.Eval(e.Row.DataItem, "myColumn").ToString();
if ((myValue == "a")) {
//Hide the row
row.Visible = false;
}
}
}
I am not sure how this will affect your paging....if you do have one.