[B] DataTable dt;
private void Permissions_Load(object sender, EventArgs e){
dt = new DataTable();
dt.Columns.Add("Feature", typeof(string));
dt.Columns.Add("None", typeof(bool));
dt.Columns.Add("ReadOnly", typeof(bool));
dt.Columns.Add("ReadWrite", typeof(bool));
datagridview1.Datasource=dt;
}
private void btnSave_Click(object sender, EventArgs e){
in the grid when i'll click on the any checkbox i want to pass parameter to this function SetPermission( , , );
parameter will be from the two enumeratons below
How manage it?
here picture Picture
}
public void SetPermission(Permission feature, PermissionType permission, int userID) {
SqlCommand cmd = new SqlCommand("Insert into Permissions (UserID,Feature,Permission)Values(@UserID,@Feature,@Permission)", con);
cmd.Parameters.Add(new SqlParameter("UserID", userID));
}
public enum Permission : int
{
Country = 0,
Store = 1,
City = 2
}
public enum PermissionType : byte
{
None = 0,
ReadOnly = 1,
ReadWrite = 2
}[/B]
styles_p 0 Newbie Poster
DdoubleD 315 Posting Shark
See MSDN documentation for more details: CellContentClick
EDIT: Removed code that didn't work!!!--sorry, I should have ran it first.
Edited by DdoubleD because: n/a
DdoubleD 315 Posting Shark
There may be a more elegant way to do this, but here is the logic I applied in a small program to demonstrate one way to get and set the checked state when the user clicks the cell's checkbox:
// This event occurs before the cell's state is committed,
// so you have to check the current value and then negate it
// to get the proposed state change...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int iCol = e.ColumnIndex;
int iRow = e.RowIndex;
if (dataGridView1.Rows[iRow].Cells[iCol].ValueType == typeof(bool))
{
if (dataGridView1.Rows[iRow].Cells[iCol].Value == null ||
(bool)dataGridView1.Rows[iRow].Cells[iCol].Value == false)
{
dataGridView1.Rows[iRow].Cells[iCol].Value = true;
}
else
{
dataGridView1.Rows[iRow].Cells[iCol].Value = false;
}
}
}
// Occurs when the value is changed...
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int iCol = e.ColumnIndex;
int iRow = e.RowIndex;
if (iRow > -1 & iCol > -1)
{
if (dataGridView1.Rows[iRow].Cells[iCol].ValueType == typeof(bool))
{
bool bChecked = (bool)dataGridView1.Rows[iRow].Cells[iCol].Value;
}
}
}
I don't know what you are doing with your SetPermission, but according to what you stated you want to do, you can call that inside the dataGridView1_CellValueChanged event if that is what you want to do.
styles_p 0 Newbie Poster
There may be a more elegant way to do this, but here is the logic I applied in a small program to demonstrate one way to get and set the checked state when the user clicks the cell's checkbox:
// This event occurs before the cell's state is committed, // so you have to check the current value and then negate it // to get the proposed state change... private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { int iCol = e.ColumnIndex; int iRow = e.RowIndex; if (dataGridView1.Rows[iRow].Cells[iCol].ValueType == typeof(bool)) { if (dataGridView1.Rows[iRow].Cells[iCol].Value == null || (bool)dataGridView1.Rows[iRow].Cells[iCol].Value == false) { dataGridView1.Rows[iRow].Cells[iCol].Value = true; } else { dataGridView1.Rows[iRow].Cells[iCol].Value = false; } } } // Occurs when the value is changed... private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { int iCol = e.ColumnIndex; int iRow = e.RowIndex; if (iRow > -1 & iCol > -1) { if (dataGridView1.Rows[iRow].Cells[iCol].ValueType == typeof(bool)) { bool bChecked = (bool)dataGridView1.Rows[iRow].Cells[iCol].Value; } } }
I don't know what you are doing with your SetPermission, but according to what you stated you want to do, you can call that inside the dataGridView1_CellValueChanged event if that is what you want to do.
well i'll try to explain it
here is link of picture
when i click on any checkbox here, I want to pass parameter in set function, for example: if I'll click on row Stores, Column read only expedient checkbox item i want to pass SetPermission(1,1, ...) because in enumeration stores id is 1 and read only's id is 1 too.
How i do it?
Edited by styles_p because: n/a
styles_p 0 Newbie Poster
Is not any idea??
DdoubleD 315 Posting Shark
const int iNoneCol = (int)PermissionType.None + 1;
const int iROCol = (int)PermissionType.ReadOnly + 1;
const int iRWCol = (int)PermissionType.ReadWrite + 1;
const int iCountryRow = (int)Permission.Country;
const int iStoreRow = (int)Permission.Store;
const int iCityRow = (int)Permission.City;
private void InitCheckBoxValues()
{
for (int iRow = iCountryRow; iRow <= iCityRow; iRow++)
{
DataGridViewCheckBoxCell cbcNone = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iNoneCol];
DataGridViewCheckBoxCell cbcRO = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iROCol];
DataGridViewCheckBoxCell cbcRW = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iRWCol];
cbcNone.Value = cbcNone.TrueValue = true;
cbcNone.FalseValue = false;
cbcRO.Value = cbcRO.FalseValue = false;
cbcRW.Value = cbcRW.FalseValue = false;
cbcRO.TrueValue = cbcRW.TrueValue = true;
}
}
// Occurs when the value is changed...
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int iCol = e.ColumnIndex;
int iRow = e.RowIndex;
if (iRow > -1 & iCol >= iNoneCol)
{
System.Diagnostics.Debug.Assert(dataGridView1.Rows[iRow].Cells[iCol].ValueType == typeof(bool));
DataGridViewCheckBoxCell cbcNone = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iNoneCol];
DataGridViewCheckBoxCell cbcRO = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iROCol];
DataGridViewCheckBoxCell cbcRW = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iRWCol];
if (iCol == iNoneCol && cbcNone.Value == cbcNone.TrueValue)
{
cbcRO.Value = cbcRO.FalseValue;
cbcRW.Value = cbcRW.FalseValue;
}
else if (iCol == iROCol && cbcRO.Value == cbcRO.TrueValue)
{
cbcNone.Value = cbcNone.FalseValue;
cbcRW.Value = cbcRW.FalseValue;
}
else if (iCol == iRWCol && cbcRW.Value == cbcRW.TrueValue)
{
cbcNone.Value = cbcNone.TrueValue;
cbcRO.Value = cbcRO.TrueValue;
}
// default to none CB if none selected...
if (!(cbcRO.Value == cbcRO.TrueValue || cbcRW.Value == cbcRW.TrueValue))
cbcNone.Value = cbcNone.TrueValue;
}
}
private void UpdatePermissions(int userID)
{
for (int iRow = iCountryRow; iRow <= iCityRow; iRow++)
{
Permission feature = (Permission)iRow;
PermissionType permission;
DataGridViewCheckBoxCell cbcNone = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iNoneCol];
DataGridViewCheckBoxCell cbcRO = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iROCol];
DataGridViewCheckBoxCell cbcRW = (DataGridViewCheckBoxCell)dataGridView1.Rows[iRow].Cells[iRWCol];
if (cbcNone.Value == cbcNone.TrueValue)
permission = PermissionType.None;
else if (cbcRO.Value == cbcRO.TrueValue)
permission = PermissionType.ReadOnly;
else if (cbcRW.Value == cbcRW.TrueValue)
permission = PermissionType.ReadWrite;
else
{
permission = PermissionType.None; // must assign something...
System.Diagnostics.Debug.Assert(false); // nothing selected!!!???
}
SetPermission(feature, permission, userID);
}
}
DdoubleD 315 Posting Shark
I was kind of in a hurry this morning and my internet connection has been flaky the last couple of days (real bad this morning). I posted that code without testing it, and there were a couple of logic errors as well as an important missing part in the handling of the CellValueChanged
event handler.
I decided to clean up the code and make it just a tad more generic too. I have also commented the code and left a couple of original lines commented out where I dropped the "Country, Store, City" enum. You should be able to see it as only a slight change in the call to your SetPermission
method parameters. I do not call the UpdatePermissions
method in this code, but it sets up the values needed for your SetPermission method, with the minor exception of the dropped enum for "Country, Store, & City", which you can change to use the code you have.
The listBox1 control is just an output for testing that the datatable binding updated the values correctly...
Regardless of how you are binding data to the gridview, I think you should be able to accomplish your task by extracting what you need from this example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PermissionType = ForumSolutions.DatabaseStuff.PermissionsTable.PermissionType;
using PermissionsTable = ForumSolutions.DatabaseStuff.PermissionsTable;
namespace ForumSolutions
{
public partial class Form_DataGridView_PermissionsTable : Form
{
DataTable dt;
const int iNoneCol = (int)PermissionType.None + 1;
const int iROCol = (int)PermissionType.ReadOnly + 1;
const int iRWCol = (int)PermissionType.ReadWrite + 1;
public Form_DataGridView_PermissionsTable()
{
InitializeComponent();
}
private void Form_DataGridView_Load(object sender, EventArgs e)
{
dataGridView1.SuspendLayout();
dt = PermissionsTable.LoadTable();
dataGridView1.DataSource = dt;
dataGridView1.ResumeLayout(true);
}
// This event occurs before the cell's state is committed,
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
// if this is a checkbox...
if (dataGridView1.Rows[r].Cells[c].ValueType == typeof(bool))
// Toggle the checked state...
dataGridView1.Rows[r].Cells[c].Value = !(bool)(dataGridView1.Rows[r].Cells[c].Value);
}
// Occurs when the value is changed...
static bool bUpdating = false; // used to turn off logic block while updating checkboxes...
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
if (r > -1 & c >= iNoneCol && !bUpdating)
{
// Prevent this event block from executing for values we are changing in here...
bUpdating = true;
// Ensure the cell being updated is a checkbox (bool)...
System.Diagnostics.Debug.Assert(dataGridView1.Rows[r].Cells[c].ValueType == typeof(bool));
// Get the cells as checkboxes...
DataGridViewCheckBoxCell cbcNone = (DataGridViewCheckBoxCell)dataGridView1.Rows[r].Cells[iNoneCol];
DataGridViewCheckBoxCell cbcRO = (DataGridViewCheckBoxCell)dataGridView1.Rows[r].Cells[iROCol];
DataGridViewCheckBoxCell cbcRW = (DataGridViewCheckBoxCell)dataGridView1.Rows[r].Cells[iRWCol];
// Toggle states as only one checkbox checked per row is allowed...
// None
if (c == iNoneCol && cbcNone.Value.Equals(true))
{
cbcRO.Value = false;
cbcRW.Value = false;
}
// Read only
else if (c == iROCol && cbcRO.Value.Equals(true))
{
cbcNone.Value = false;
cbcRW.Value = false;
}
// Read Write
else if (c == iRWCol && cbcRW.Value.Equals(true))
{
cbcNone.Value = false;
cbcRO.Value = false;
}
// default to None if not Read or Write selected...
if (!(cbcRO.Value.Equals(true) || cbcRW.Value.Equals(true)))
cbcNone.Value = true;
// Allow normal event operation to continue...
bUpdating = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
textBox1.AppendText(
dr["Property"] + "\t" +
dr["None"] + "\t" +
dr["Read_Only"] + "\t" +
dr["Read_Write"] + "\r\n");
}
textBox1.AppendText("\r\n");
}
private void button2_Click(object sender, EventArgs e)
{
if (!PermissionsTable.SaveTable(dt))
MessageBox.Show("Error saving table...");
}
private void UpdatePermissions(int userID)
{
//for (int iRow = iCountryRow; iRow <= iCityRow; iRow++)
for (int r=0; r<dt.Rows.Count; r++)
{
DataRow dr = dt.Rows[r];
//Permission feature = (Permission)iRow;
PermissionType permissionType;
if (dr["None"].Equals(true))
permissionType = PermissionType.None;
else if (dr["Read_Only"].Equals(true))
permissionType = PermissionType.ReadOnly;
else if (dr["Read_Write"].Equals(true))
permissionType = PermissionType.ReadWrite;
else
{
System.Diagnostics.Debug.Assert(false); // should not be here!!!
permissionType = PermissionType.None; // must assign something...
}
string feature = "Object " + r;
SetPermission(feature, permissionType, userID);
}
}
//public void SetPermission(Permission feature, PermissionType permission, int userID)
public void SetPermission(string objName, PermissionType permission, int userID)
{
}
}
}
And, here is what I have defined for loading my DataTable:
public class DatabaseStuff
{
public class PermissionsTable
{
public enum PermissionType : byte
{
None = 0,
ReadOnly = 1,
ReadWrite = 2
}
public static DataTable LoadTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Property", typeof(string));
dt.Columns.Add("None", typeof(bool));
dt.Columns.Add("Read_Only", typeof(bool));
dt.Columns.Add("Read_Write", typeof(bool));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["Property"] = "Object " + i;
dr["None"] = true;
dr["Read_Only"] = false;
dr["Read_Write"] = false;
dt.Rows.Add(dr);
}
return dt;
}
public static bool SaveTable(DataTable dt)
{
return true;
}
}
Cheers!
sknake commented: great post +5
styles_p 0 Newbie Poster
DdoubleD
at first thank you for this interesting code, it was ver usefull for me
what's abou my code I think it works...
In setPermission Function there are both insert an update statments
here code
public partial class Permissions : Form
{
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["G1_StoreHouse.Properties.Settings.StoreHouseConnectionString"].ConnectionString);
DataTable dt;
DataGridViewCheckBoxCell cbcNone;
DataGridViewCheckBoxCell cbcRO;
DataGridViewCheckBoxCell cbcRW;
const int noneCol = (int)PermissionType.None + 1;
const int roCol = (int)PermissionType.ReadOnly + 1;
const int rwCol = (int)PermissionType.ReadWrite + 1;
const int countryRow = (int)Permission.Country;
const int storeRow = (int)Permission.Store;
const int cityRow = (int)Permission.City;
public Permissions()
{
InitializeComponent();
}
private void Permissions_Load(object sender, EventArgs e)
{
this.users_VTableAdapter.Fill(this.usersDS.Users_V);
dt = new DataTable();
dt.Columns.Add("Feature", typeof(string));
dt.Columns.Add("None", typeof(bool));
dt.Columns.Add("ReadOnly", typeof(bool));
dt.Columns.Add("ReadWrite", typeof(bool));
cmbUsers.Text = "--(Pelase Select)--";
foreach (string item in Enum.GetNames(typeof(Permission)))
{
dt.Rows.Add(item, true, false, false);
}
dataGridView1.DataSource = dt;
}
public void SetPermission(Permission feature, PermissionType permission, int userID, int i)
{
SqlCommand cmd1 = new SqlCommand("Delete from Permissions where UserID=@UserID", con);
cmd1.Parameters.Add(new SqlParameter("UserID", userID));
SqlCommand cmd = new SqlCommand(@"Insert into Permissions
(UserID,Feature,Permission)
Values
(@UserID,@Feature,@Permission)", con);
cmd.Parameters.Add(new SqlParameter("UserID", userID));
cmd.Parameters.Add(new SqlParameter("Feature", (int)feature));
cmd.Parameters.Add(new SqlParameter("Permission", (int)permission));
if (i == 0)
{
cmd1.Connection.Open();
cmd1.ExecuteNonQuery();
cmd1.Connection.Close();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
else
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
}
public PermissionType GetPermission(Permission feature, int userID)
{
SqlCommand cmd = new SqlCommand("select Permission from Permissions where UserID=@UserID and Feature=@Feature", con);
cmd.Parameters.Add(new SqlParameter("UserID", userID));
cmd.Parameters.Add(new SqlParameter("Feature", (int)feature));
try
{
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
return (PermissionType)dr[0];
}
else
{
return PermissionType.None;
}
}
finally
{
cmd.Connection.Close();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
int i = 0;
for (int Row = countryRow; Row <= cityRow; Row++)
{
Permission feature = (Permission)Row;
PermissionType permission = new PermissionType();
bool cbcNone = (bool)dataGridView1.Rows[Row].Cells[noneCol].Value;
bool cbcRO = (bool)dataGridView1.Rows[Row].Cells[roCol].Value;
bool cbcRW = (bool)dataGridView1.Rows[Row].Cells[rwCol].Value;
if (cbcNone)
permission = PermissionType.None;
if (cbcRO)
permission = PermissionType.ReadOnly;
if (cbcRW)
permission = PermissionType.ReadWrite;
int k = i++;
SetPermission(feature, permission, (int)cmbUsers.SelectedValue, k);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
if (dataGridView1.Rows[r].Cells[c].ValueType == typeof(bool))
dataGridView1.Rows[r].Cells[c].Value = !(bool)(dataGridView1.Rows[r].Cells[c].Value);
cbcNone = (DataGridViewCheckBoxCell)dataGridView1.Rows[r].Cells[noneCol];
cbcRO = (DataGridViewCheckBoxCell)dataGridView1.Rows[r].Cells[roCol];
cbcRW = (DataGridViewCheckBoxCell)dataGridView1.Rows[r].Cells[rwCol];
if (c == noneCol && cbcNone.Value.Equals(true))
{
cbcRO.Value = false;
cbcRW.Value = false;
}
else if (c == roCol && cbcRO.Value.Equals(true))
{
cbcNone.Value = false;
cbcRW.Value = false;
}
else if (c == rwCol && cbcRW.Value.Equals(true))
{
cbcNone.Value = false;
cbcRO.Value = false;
}
}
private void cmbUsers_SelectionChangeCommitted(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("Feature", typeof(string));
dt.Columns.Add("None", typeof(bool));
dt.Columns.Add("ReadOnly", typeof(bool));
dt.Columns.Add("ReadWrite", typeof(bool));
foreach (string item in Enum.GetNames(typeof(Permission)))
{
Permission s = (Permission)Enum.Parse(typeof(Permission), item);
PermissionType pt = GetPermission((Permission)Enum.Parse(typeof(Permission), item), (int)cmbUsers.SelectedValue);
dt.Rows.Add(item, pt == PermissionType.None, pt == PermissionType.ReadOnly, pt == PermissionType.ReadWrite);
}
dataGridView1.DataSource = dt;
}
}
public enum Permission : int
{
Country = 0,
Store = 1,
City = 2
}
public enum PermissionType : byte
{
None = 0,
ReadOnly = 1,
ReadWrite = 2
}
DdoubleD 315 Posting Shark
Sounds like you have it the way you want--great! Please mark as solved if you have your solution.
Cheers!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.