problem with datatable [modified]
-
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
}C# Developer
modified on Monday, October 26, 2009 3:18 PM
-
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
}C# Developer
modified on Monday, October 26, 2009 3:18 PM
From reading your question, which was slightly difficult because your actual question is mixed up in the middle of your code, I would suggest that you redesign your DataTable
private void Permissions\_Load(object sender, EventArgs e){ dt = new DataTable(); dt.Columns.Add("Feature", typeof(string)); dt.Columns.Add("None", typeof(bool)); //<======================== Here =============== dt.Columns.Add("ReadOnly", typeof(bool)); //<======================== Here =============== dt.Columns.Add("ReadWrite", typeof(bool)); //<======================== Here =============== datagridview1.Datasource=dt; }
The three columns marked //<======================== Here =============== are the three possible values of
PermissionType
and should be one column, probably, without thinking about it too much, of typeint
. This would mean that the design of your Form and Grid would need to change which would therefore probably change the question. Also you refer in the question bit to the any checkbox without having previously mentioned it, let alone describe its purpose.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
From reading your question, which was slightly difficult because your actual question is mixed up in the middle of your code, I would suggest that you redesign your DataTable
private void Permissions\_Load(object sender, EventArgs e){ dt = new DataTable(); dt.Columns.Add("Feature", typeof(string)); dt.Columns.Add("None", typeof(bool)); //<======================== Here =============== dt.Columns.Add("ReadOnly", typeof(bool)); //<======================== Here =============== dt.Columns.Add("ReadWrite", typeof(bool)); //<======================== Here =============== datagridview1.Datasource=dt; }
The three columns marked //<======================== Here =============== are the three possible values of
PermissionType
and should be one column, probably, without thinking about it too much, of typeint
. This would mean that the design of your Form and Grid would need to change which would therefore probably change the question. Also you refer in the question bit to the any checkbox without having previously mentioned it, let alone describe its purpose.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Henry Minute I'll try to explain it Here is link of Picture http://img9.imageshack.us/i/35025568.jpg/ When I click on any checkbox here,I want to pass parameter in set function, for example like this: if I'll click on row Stores, Column read only's 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.
C# Developer
-
Henry Minute I'll try to explain it Here is link of Picture http://img9.imageshack.us/i/35025568.jpg/ When I click on any checkbox here,I want to pass parameter in set function, for example like this: if I'll click on row Stores, Column read only's 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.
C# Developer
I am not able to view your image, as I am having bandwidth problems and it would take about 20mins to load (I probably wouldn't anyway - no reflection on yourself but I am very distrustful of following links. :) ) My basic problem with the code you presented originally is that nowhere in it do you load any existing data. I can only assume therefore that the part you are working on is for new data only. On that basis if you must use a grid it would make things a whole lot easier to code for if, instead of 3 (or however many options there are)
CheckBoxColumn
s, you had 1ComboBoxColumn
with its items set to the possible options. What would happen if the number of options suddenly increased to 20+, are you going to add that number of additionalCheckBoxColumn
s? In any event, as soon as the user makes a selection you are saving the data, so why use a grid? It would be far, far easier to simply have appropriate controls on aForm
orPanel
and clear them on a successful save ready for the next entry and leave them populated on any error so that corrections can be made. Sorry not to be more help, but it really seems to me that your design needs some more thought. :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
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
}C# Developer
modified on Monday, October 26, 2009 3:18 PM
Try the following. It may help you. Remember that you have to initialize check box values at first.
private void Form1_Load(object sender, EventArgs e) { int p = dataGridView1.Rows.Add(); dataGridView1.Rows[p].Cells[0].Value = true.ToString(); dataGridView1.Rows[p].Cells[1].Value = false.ToString(); }
Handle the following event of datagridview
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals(false.ToString())) { dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true.ToString(); MessageBox.Show("true"); } else { dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false.ToString(); MessageBox.Show("false"); } }
call the method in appropriate if else condition.