Hi @Padmanabhan, Venkatesh ,Welcome to Q&A.
Create a config file first, and then get its AppSettingsSection.
Get the corresponding value by looping dt.rows and using the value of the name column of row as the key. In this way you can compare it well.
You could try the following code to get what you wanted.
using System;
using System.Configuration;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettings = config.AppSettings;
foreach (DataRow row in dt.Rows)
{
string name = row["name"].ToString();
int value = Convert.ToInt32(row["value"]);
// Retrieve the value from appSettings for the corresponding key
string appSettingValue = appSettings.Settings[name]?.Value;
if (!string.IsNullOrEmpty(appSettingValue) && value <= Convert.ToInt32(appSettingValue))
{
Console.WriteLine($"For name: {name}, value: {value} is less than or equal to appSetting value: {appSettingValue}");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Create a datatable containing name and value
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("value", typeof(int));
dt.Rows.Add("Joe", 60);
dt.Rows.Add("Richard", 90);
dt.Rows.Add("Mary", 60);
dataGridView1.DataSource = dt;
}
}
}
<configuration>
<appSettings>
<add key="Joe" value="55" />
<add key="Richard" value="90" />
</appSettings>
</configuration>
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.