I am working on authorization project and I want to write code to enable authorized buttons with for loop while opening form.
Authorizated buttons cames from sql database
row.Enabled = true;
gives me this error;
"datarow does not contain a definition for "Enabled" and no extension method 'Enabled' accepting a
first argument of type 'DataRow' could be found(are you missing a
directive reference or an assembly reference?)"
This is my code
public void yetkiver()
{
SqlDataAdapter adapter1903 = new SqlDataAdapter();
SqlCommand cmd1903 = new SqlCommand("select distinct buttonname from authorization where Isactive=1 and usercode='user1'", con);
DataTable dt1903 = new DataTable();
DataSet ds1903 = new DataSet();
adapter1903.SelectCommand = cmd1903;
adapter1903.Fill(ds1903);
dt1903 = ds1903.Tables[0];
foreach (DataColumn col in dt1903.Columns)
{
foreach (DataRow row in dt1903.Rows)
{
row.Enabled = true;
}
}
}
Of course a DataRow
has no Enabled
property. You want to enable the button with this name. So you have to find it on the form. You can use Controls.Find
and LINQ:
foreach (DataRow row in dt1903.Rows)
{
string buttonName = row.Field<string>("buttonname");
Button btn = this.Controls.Find(buttonName, true).OfType<Button>().SingleOrDefault(); // throws an exception if multiple found
if(btn != null)
btn.Enaled = true;
}
This is a weird approach by the way, what if someone wants to change the button-names? I would not store the names in database but their type/meaning. Don't use a loop to find them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.