How to access properties of GridView Templete Field in Web Forms - c#

I have .NET project on WebForms and GridView in it. GridView is populated with data from my DB. The DB has column named password but on GridView this column is set to Visible="False". When i click command button Edit all boxes are ready to be filled with new data, but column password is invisible.
My question is: when i click edit, how to (or can i) make column with passwords visible and ready for filling text box with new passwords, but also i don't want to show other password of course. Summary can i access somehow properties of this TempleteField/ItemTemplete ?

No need to set the Visible="false" property. Just trick the GridView to hide a column in normal display mode and show the column when in edit mode.
Assuming Password Column is the third column, Handle the RowDataBound event of GridView to hide this Column:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Just return in case GridView is rendering in Edit Mode
if (GridView1.EditIndex >= 0)
return;
// In case GridView is rendering in Normal state
// Hide the Columns you don't want to display
if ((e.Row.RowState == DataControlRowState.Normal ||
e.Row.RowState == DataControlRowState.Alternate) &&
( e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header))
{
// Hide the Password Column
e.Row.Cells[2].Visible = false;
}
}
In case you use TemplateField for your Password Column, you can hide it by using the DataBound event of GridView as:
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.EditIndex >= 0)
return;
GridView1.Columns[2].Visible = false;
}
Now when the GridView goes to Edit Mode ( on click of Edit Button etc..) it will display the Password column in Edit Mode.

Related

Regarding gridview in asp.net

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

ASP.NET GridView allow to edit certain row

Is there a way to allow editing of certain row instead of the whole row?
When I search a user it will fill data into the GridView. If I set the AutoGenerateEditButton to True, all the row will be editable. For example, if the GridView has 10 row, when the data in the row is between a certain criteria using If-else statement, I only want the bottom 5 to be editable instead of the whole 10 row.
try this..On gridviews row data bound event check the criteria ..
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ("Your Condition")
{
if ("your condition")
{
e.Row.Enabled = true;//row is editable
}
else
{
e.Row.Enabled = false;//row is not editable
}
}
}

Change GridView cells strings on Page_Load

I have a GridView that has a column called Active which shows either a 1 (for active) or a -1 for inactive.
However as this is being implemented into a front end UI I do not want users to be presented with what seems to them as useless integers, however a Active or Not active to be presented in the GridView on Page_Load.
The code would look something like -
protected void Page_Load(object sender, EventArgs e)
{
//code here to modify the column 'Active' in the GridView
//GridView ID="GV1"
if (row.Cells[1].Text == "1")
{
row.Cells[1].Text = "Active";
}
if (row.Cells[1].Text == "-1")
{
row.Cells[1].Text = "Not Active";
}
}
And the column in the GridView is -
<asp:BoundField HeaderText="Active" DataField="Active" SortExpression="Active"></asp:BoundField>
How can I do this as I do not want to edit the database in order for the UI to be more presentable?
Try the OnRowDataBound Gridview event.

How to change the color of label depending on certain values in the gridview

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

How to get an editor for a column in AutoFilterRow (devexpress xtragridview)?

I am using XtraGrid control, with GridView. I have AutoFilterRow enabled.
For text columns, I do a LIKE search, and for a dropdown column, I assigned ColumnEdit = ReositoryItemComboBox.
It appears for this column in all the rows except the AutoFilterRow. How to get it in AutoFilterRow as well ?
To display the editor for the auto filer row, handle the CustomRowCellEdit event as shown below:
void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
GridView view = sender as GridView;
if(e.Column.FieldName == "COMM_TYPE" && view.IsFilterRow(e.RowHandle))
e.RepositoryItem = repositoryItemComboBox1;
}

Categories