Disable checkbox column based on user authorization - c#

I have a grid and in the grid I have two Item Template columns - Checkbox 1 and Checkbox 2.
I want to disable the whole Checkbox 1 column based on if a user is an admin or general user.
So say, if a user is an 'admin', I want both columns to be enabled and user can check/uncheck both the columns.
But, if a user is 'general user', I want to disable 'Checkbox1', so that user can only check/uncheck Checkbox 2. But can see values for 'Checkbox 1' column and can't edit it or make changes.
Presently, I am achieving this behavior as below-
protected void grdchkbox_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkbox1 = (e.Row.FindControl("Checkbox1") as CheckBox);
if (IsGeneralUser(empid))
{
chkbox1.Enabled = false;
}
}
}
The problem with this is the page load time.
It is checking for user's role everytime a row is bound which means making db calls at each row bound.
Also, it's disabling a checkbox for each row separately.
Is there a way, I can disable the whole checkbox 1 column in one go?

depending on how your gridview is setup, you could try just disabling editing on the column or making it read only.
You could also simply save the users status in a session variable, so that you only have to make the db call the first time, which should speed up the whole process as well.
Using a session variable would look something like:
protected void grdchkbox_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkbox1 = (e.Row.FindControl("Checkbox1") as CheckBox);
//we check to see if we already have the users status saved in session
var isUser = HttpContext.Current.Session["IsGeneralUser"];
//if we dont, we run the IsGeneralUser method and save the result in session
if(isUser == null)
{
isUser = IsGeneralUser(empid);
HttpContext.Current.Session.Add("IsGeneralUser", isUser);
}
if (Convert.ToBoolean(isUser))
{
chkbox1.Enabled = false;
}
}
}

Related

Prevent first cell of DataGridView from being selected when row header clicked

I need to allow the user to select a row by clicking on the row header to allow row deletion, but when the row header is clicked, the first cell gets focus and the text is highlighted, which I need to prevent so the user can't make accidental edits to that first cell.
Here's an example of what's happening:
As you can see, the first cell has focus and the user can edit that cell, when all that was done was the row header was clicked.
I tried the answer to this SO question, but neither event fired when I clicked a row header. Here's the code that I used for that attempt:
private void dgvCategories_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
Console.WriteLine("true");
dgvCategories.ReadOnly = true;
}
}
private void dgvCategories_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
Console.WriteLine("false");
dgvCategories.ReadOnly = false;
}
}
I also tried experimenting with the various DataGridViewEditMode options, but none of those accomplished what I need.
The DGV SelectionMode is set to RowHeaderSelect.
I still need to be able to grab the hidden "id" field in the row for the delete process. Here's my code for that:
private void btnDeleteRow_Click(object sender, EventArgs e)
{
int index = dgvCategories.SelectedRows[0].Index;
string rowId = dgvCategories[0, index].Value.ToString();
string deleteString = string.Format("DELETE FROM masterfiles.xref WHERE id = " + rowId);
conn.Open();
NpgsqlCommand deleteCmd = new NpgsqlCommand(deleteString, conn);
deleteCmd.ExecuteNonQuery();
conn.Close();
}
That works; I just need to prevent that first cell from automatically getting focus for editing when the row is selected. I still need to allow the user to select individual cells for editing, as well.
Try :
1- make the DataGridView selection by one row instead of one cell .
2- Disable allowing user to edit the DataGridView.
3- Make DataGridView ReadOnly .
Do 1 & 2 all the time except at editing operation .

disable dropdownlist option after selection asp.net webform

I want to disable my dropdownlist after a user select the droopdownlist. So, if the user enter this page, the user only lead to
dropdownlist when they have not choose before
the selected item value if they have choose before, but they can not select again (means: it disabled)
http://i.stack.imgur.com/H7uYT.png
because every selected item here will trigger some calculation to the next page.
Do you have any tutorial to do this? Or can you help me to solve this?
Just set the DowpDownList's AutoPostBack property to true and handle the SelectedIndexChanged-event. There you can disable it:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
ddl.Enabled = false;
// store the SelectedValue somewhere, f.e. in the Session
// or Response.Redirect to the next page and pass it via url-parameter
}

How to hide Rows In GridView

I have came across the we and check different web pages but I didnt find the one I was looking for. I have a gridview and all I want is to hide one of the rows based on the value in the cell.
What I need to happen is something like in the logic of this :
if (row = "someValue")
{
row.Visible = false;
}
for the record, I have tried this but no luck:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string oRoleName = row.Field<string> ("SVal");
if (oRoleName.Equals ("someValue")) {
e.Row.Visible = false;
}
}
It is not base weather the row is the first the 2nd or third (like: e.row[1], e.row[2], etc.) I need to filter the data base on the value in the row. Can anyone teach me how could this be done ?
Would appreciate any help.
In a RowDataBound event add something along the lines of this logic
if (e.Row.Cells[5].Text == "foo") {
e.Row.Visible = false;
}
EDIT:
If youre looking to check the value of each row as its entered (unless im understanding incorrectly, you should probably expand on your question a bit.)
Then you may want to use the event "CellValueChanged"
Check to see if the cell is null beforehand and then do the check for your value and apply logic accordingly below that.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell != null) {
if (dataGridView1.CurrentCell.Value.ToString() == "foo")
{
// do your stuff here.
}
}
}
What you need to do is get the value of the column within the GridViewRow. You are on the right track. In RowDataBound, find the column you are after. Then check its value.
Here is an example. Use FindControl() to get the control in the specified column for the current row. If the control in that column is a Label, check the text of the label to see if it is the value you want hidden. If so, hide the row.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("MyLabel");
if (lbl.Text == "MyValue")
{
e.Row.Visible = false;
}
}
}

How to hide gridview column after databind?

I hide my columns using the solution in following link
How to hide a TemplateField column in a GridView
However this causes problems with update operations, as gridview acts as hidden rows has null value. So how to hide columns after databind?
protected void begv_OrderDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)begv_OrderDetail.Columns.Cast<DataControlField>().Where(fld => fld.HeaderText == "FileNo").SingleOrDefault()).Visible = "False";
}
Try this,
grid1.Columns[columnIndex].Visible= false;
Edit based on comment of questioner, for getting values of hidden columns
You can use hidden fields to store column wise values. This article has example that will help how to use hidden fields.
Instead of hiding column you can put the data of columns in datakeynames and later access those values. This will be useful in grabbing how to use DataKeyNames. By this method you may need to pass the id from data key names and get the record.
try this example, (i don't speak english)
into RowDataBound ...
protected void gvTeste_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
teste listaTeste = e.Row.DataItem as ListaTeste;
if (listaTeste.ID == 0)
{
e.Row.Cells[2].Text = "Não Atribuido";
}
if (e.Row.Cells[7].Text == "01/01/0001" || e.Row.Cells[8].Text == "01/01/0001")
{
**e.Row.Visible = false;** // disable row
}
}
}

How to disable a row by selecting value from comboBox in datagridview?

Here I have a column name "Status" in which I am using ComboBox whose values are "Pending" and "Delivered" now I want that when a user selects the delivered from the ComboBox then the entire row or this control should be disabled so user could not change it again and by default its value should be "Pending" how can i do it? Is it possible to disable a single row in gridview?
You cannot disable an individual row. However you can make it readonly:
DataGridView1.Rows[rowIndex].ReadOnly = true;
(This makes row with index of rowIndex set to readonly).
For your specific example, you would want to handle the CellValueChanged event of the datagridview and have code along the lines of:
void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4 && DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Delivered")
{
DataGridView1.Rows[e.RowIndex].ReadOnly = true;
}
}
You can handle the RowChanged event and do something like
if (Status == 'Delivered')
{
e.Row.Enabled = False;
}

Categories

Resources