| | |
problem in datagrid form
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 10
Reputation:
Solved Threads: 0
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
}•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
0
#2 Oct 26th, 2009
See MSDN documentation for more details: CellContentClick
EDIT: Removed code that didn't work!!!--sorry, I should have ran it first.
EDIT: Removed code that didn't work!!!--sorry, I should have ran it first.
Last edited by DdoubleD; Oct 26th, 2009 at 10:02 pm.
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
1
#3 Oct 26th, 2009
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:
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.
C# Syntax (Toggle Plain Text)
// 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.
•
•
Join Date: Mar 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#4 Oct 27th, 2009
•
•
•
•
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:
C# Syntax (Toggle Plain Text)
// 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.
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?
Last edited by styles_p; Oct 27th, 2009 at 6:44 am.
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
0
#6 Oct 27th, 2009
C# Syntax (Toggle Plain Text)
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); } }
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
1
#7 Oct 28th, 2009
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
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
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:
And, here is what I have defined for loading my DataTable:
Cheers!
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:
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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!
•
•
Join Date: Mar 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#8 Nov 1st, 2009
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
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
C# Syntax (Toggle Plain Text)
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 }
![]() |
Similar Threads
- Help with datagrid (VB.NET)
- Problem on Datagrid (ASP.NET)
- add column to datagrid form dropdownlist selectitem (VB.NET)
- Datagrid font display problem. (Visual Basic 4 / 5 / 6)
- ASP Form problem - email not coming through (ASP)
- problem with opening form (Visual Basic 4 / 5 / 6)
- Problem with closing form while clicking 'X' (C#)
- How to Show Database In DataGrid Using DAO (Visual Basic 4 / 5 / 6)
- Displaying datagrid on second form (VB.NET)
- Blank Record & Open Form From Datagrid (C#)
Other Threads in the C# Forum
- Previous Thread: sqlException--invalid expression
- Next Thread: Tables
Views: 467 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gcd gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





