I cannot see what the codes mean here. The variable "tulevel2" and "tuperimeter2" will be always the same with "ulevel" and "uperimeter" respectively. Moreover, you should not instantiate
a "DataTable dt" in while-loop. As you can see, in the next loop, the previous data table will not kept.
Could you please provide us with below information so that we could help you solve the problem?
the aspx markup so that we know what exactly layout you mean.
the rule when the check box will be enabled (or sample data and result).
If you want to enable a check box in one row of the gridview, you might need to think more about using Row event. For example,
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
That is because you add the 'p_ulevel' column for twice.
Without more information, I could just modify your codes as below but I am not sure it is 100% suitable for your project.
// instantiate the datatable and add two columns
DataTable dt = new DataTable();
dt.Columns.Add("p_ulevel", typeof(string));
dt.Columns.Add("p_uperimeter", typeof(string));
while (reader.Read())
{
//the first cell in current row
ulevel = reader.GetString(0);
// the second cell in current row
uperimeter = reader.GetString(1);
dt.Rows.Add(new object[] { ulevel, uperimeter });
if (Convert.ToInt32(ulevel) == 1)
{
chkRow.Enabled = true;
}
else
{
chkRow.Enabled = false;
}
}
Above codes should at least perform the same as the previous.
tuperimeter2.ToString().Contains(tuperimeter.ToString()) will always return 'true' because below codes add the same values in the data table and read them out. If you compare them with original one, it definitely give you a 'true'.
var tulevel2 = dt.AsEnumerable().Select(z => z.Field<string>("p_ulevel")).FirstOrDefault(s => s.Contains(ulevel));
var tuperimeter2 = dt.AsEnumerable().Select(z => z.Field<string>("p_uperimeter")).FirstOrDefault(s => s.Contains(uperimeter));
I guess chkRow is the check box control in the grid view row however you might only be able to access it in 'RowXXX' event handler of the gridview. If it is not inside the grid view control, you just change it state multiple times and keep
its state for the last value.
You could post more related codes, e.g. aspx page markup, other codes behind.
We would help you implement this functionality with these information.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
63 Points
129 Posts
Enabled or disabled checkbox in gridview using datatable in C# ASPNET
Jan 21, 2021 09:59 AM|Golia|LINK
Hi,
In my mysql table the users enabled are divided by
level
andperimeter
+---------+--------+------------+ | uname | ulevel | uperimeter | +---------+--------+------------+ | 5207459 | 1 | 400X | | 5204759 | 1 | 4SXR | +---------+--------+------------+
I need to enable a
checkbox
in thegridview
for each user, when your level is1
value and for your enablingperimeter
.This is my code-behind on C# ASPNET.
But for the
uname 5207459
ingridview
is enabled only the checkbox withuperimeter 4SXR
, instead ofuperimeter 400X
anduperimeter 4SXR
.Help me to do it.
while (reader.Read()) { ulevel = reader.GetString(0); uperimeter = reader.GetString(1); DataTable dt = new DataTable(); dt.Columns.Add("p_ulevel", typeof(string)); dt.Columns.Add("p_uperimeter", typeof(string)); dt.Rows.Add(new object[] { ulevel, uperimeter }); var tulevel2 = dt.AsEnumerable().Select(z => z.Field<string>("p_ulevel")).FirstOrDefault(s => s.Contains(ulevel)); var tuperimeter2 = dt.AsEnumerable().Select(z => z.Field<string>("p_uperimeter")).FirstOrDefault(s => s.Contains(uperimeter)); if (Convert.ToInt32(tulevel2) == 1 && tuperimeter2.ToString().Contains(tuperimeter.ToString())) { chkRow.Enabled = true; } else { chkRow.Enabled = false; } }
Contributor
2840 Points
840 Posts
Re: Enabled or disabled checkbox in gridview using datatable in C# ASPNET
Jan 22, 2021 07:13 AM|Sean Fang|LINK
Hi Golia,
How we could decide enable the checkbox by the uperimeter column?
You need to provide us with this information.
Regarding the below codes,
I cannot see what the codes mean here. The variable "tulevel2" and "tuperimeter2" will be always the same with "ulevel" and "uperimeter" respectively. Moreover, you should not instantiate a "DataTable dt" in while-loop. As you can see, in the next loop, the previous data table will not kept.
Could you please provide us with below information so that we could help you solve the problem?
If you want to enable a check box in one row of the gridview, you might need to think more about using Row event. For example,
GridView.RowDataBound Event
Looking forward to your reply.
Best regards,
Sean
Member
63 Points
129 Posts
Re: Enabled or disabled checkbox in gridview using datatable in C# ASPNET
Jan 23, 2021 06:54 AM|Golia|LINK
Thanks for reply.
But if a "DataTable dt" is instantiate outside from while-loop... I have this error:
Contributor
2840 Points
840 Posts
Re: Enabled or disabled checkbox in gridview using datatable in C# ASPNET
Jan 25, 2021 09:17 AM|Sean Fang|LINK
Hi Golia,
That is because you add the 'p_ulevel' column for twice.
Without more information, I could just modify your codes as below but I am not sure it is 100% suitable for your project.
Above codes should at least perform the same as the previous.
var tulevel2 = dt.AsEnumerable().Select(z => z.Field<string>("p_ulevel")).FirstOrDefault(s => s.Contains(ulevel)); var tuperimeter2 = dt.AsEnumerable().Select(z => z.Field<string>("p_uperimeter")).FirstOrDefault(s => s.Contains(uperimeter));
You could post more related codes, e.g. aspx page markup, other codes behind.
We would help you implement this functionality with these information.
Best regards,
Sean
Member
63 Points
129 Posts
Re: Enabled or disabled checkbox in gridview using datatable in C# ASPNET
Jan 25, 2021 09:37 PM|Golia|LINK
Many thanks for this help.
Now working correctly using your last suggestion.