更新时间:2023-12-01 22:37:28
CurrentCell与使用SelectedRows属性的所选单元格不同,并从那里访问用户名列。
这有效:private 无效对接on5_Click( object sender,EventArgs e)
{
string tempusername;
if (log_InDataGridView.Rows.Count > 1 )
{
if (log_InDataGridView.Rows [log_InDataGridView.CurrentRow.Index] .IsNewRow!= true )
{
tempusername = log_InDataGridView.CurrentRow.Cells [ 0 ] .Value.ToString( ).Trim()ToLower将();
username = username.Trim()。ToLower();
if (tempusername.Equals(username)== true )
{
MessageBox.Show( 无法删除当前用户);
}
else
this .log_InBindingSource.RemoveCurrent();
}
}
else
MessageBox.Show( 请先选择要删除的用户);
}
I have a data table contains username and password and type, and a gridview to edit users. Now when I click the delete user button I want it to check if the current user name and selected rows user name is same or not. I have a string called kullaniciadi where I keep last loged in username from textbox1.text like this:
kullaniciadi = UserNametextBox1.Text;
And I tryed this but well it doesn't work for apearently obvious reasons couse I couldn't find anything on the internet about this:
if ( log_InDataGridView.CurrentCell.Value.ToString == kullaniciadi)
MessageBox.Show ( "Cannot delete currently loged in user" );
How can I do this?
Thanx.
The CurrentCell is not the same as the selected cell- try using the SelectedRows property instead and access the username column from there.
This worked:private void button5_Click ( object sender , EventArgs e ) { string tempusername; if ( log_InDataGridView.Rows.Count > 1 ) { if ( log_InDataGridView.Rows [ log_InDataGridView.CurrentRow.Index ].IsNewRow != true ) { tempusername= log_InDataGridView.CurrentRow.Cells [ 0 ].Value.ToString ().Trim().ToLower(); username= username.Trim ().ToLower (); if( tempusername.Equals(username)==true) { MessageBox.Show ( "Cannot delete currently loged in user" ); } else this.log_InBindingSource.RemoveCurrent (); } } else MessageBox.Show ( "Please select a user to delete first" ); }