If checkbox is checked in datagrid in C#
On form2 in a datagrid field, I have "admin" which is a checkbox. When I call form2 from form1 I want it first to check if THIS user (which is logged in) has checked in field "admin". If yes, grant the user access to form2, else return to form1 with a message box explaining that the user doesn't have access.
Should the check "if has checked" be on form2 load?
EDIT: Can it be done like:
form1:
call form2 function test
form2:
function test that checks if the current user has checked the checkbox i开发者_开发知识库n the datagrid
No, if you perform the check in Load, then you will probably have trouble preventing the form from appearing.
Instead, put method on Form1 that will perform the check or show Form2? This way all your code which would want to display Form2 can benefit from the same check.
public class Form1
{
...
public void ShowAdminForm2()
{
if (!chkAdmin.IsChecked)
MessageBox.Show ("Not admin");
else
new Form2().ShowDialog();
}
}
From what I understand to what your asking, there are various ways you can do that. You could check on the load event of form2 if user has admin access, or you could perform the check from form1 and check if user has admin access before creating an instance of form2.
There are two options. First, read the state from DataGrid to a CheckBox like:
CheckBox.Checked = GridView.Rows[0].Cells[Index.Of.CheckBox]
Or you should TypeCast:
if ((CheckBox)GridView.Rows[0].Cells[Index.Of.CheckBox]).Checked)
DoWhatEverYouWant
精彩评论