我有 ac# 项目,有 4 个以上的表单(父子关系)。在 Form01 中,我创建了一个数据表,其中的初始“Rating”值分配给变量“Horse”:
public Form01()
{
InitializeComponent();
dgvMeetings.DataSource = GetMeetings();
dtRatings = GetRating();
}
dtRatings 表由 SQL 调用生成,如下所示:
public DataTable GetRating()
{
DataTable dtRating = new DataTable();
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(@"SELECT
Course
, Race
, Cloth
, Horse
, Rating
, Odds
FROM[Smartform].[dbo].[Entries]
where Date = convert(date, getdate() - 1)
order by 1, 2, 3", con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dtRating.Load(reader);
}
}
return dtRating;
}
在 Form4 中,我希望能够通过在文本框中输入值来更新 dtRating 中的“Rating”值。我一直试图通过 textbox-keyup 调用来做到这一点,如下所示:
private void TbxForm01_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
string searchExpression = "Horse = " + Form03.Horse;
MessageBox.Show(searchExpression);
DataRow[] foundRows;
foundRows = Form01.dtRatings.Select(searchExpression);
MessageBox.Show(foundRows.ToString());
}
}
正如你所看到的,我才刚刚开始。我的计划是使用 Form03.Horse 在 dtRatings 上查找 Horse 并检索行号。然后使用它用文本框值覆盖评级值。
第一个消息框显示正确的“马”名称。但是,我无法让 foundrows 表达式工作并从 dtRatings 正确检索行号。
任何人都可以帮助解决这个问题并告诉我如何使用文本框输入的值覆盖 dtRatings 中的评级值?
谢谢。
我想你想要做的是有一个表格来输入马#/名称,并尝试从数据表中查找这匹马。这将做到这一点。但是,您将留下 DataRow,您将需要一个名为 txtRating 的 TextBox,其中包含一个数字,例如 4.3,您要从中更新:
在 Form03 中:
private void txtHorse_KeyDown(object sender, KeyEventArgs e)
{
// If we hit enter and we have a horse value in the text box, look it up
if (e.KeyCode == Keys.Enter && txtHorse.Text.Length > 0)
{
foreach (DataRow dt in Form01.dtRating.Rows)
{
if (dt["Horse"].ToString() == txtHorse.Text)
{
// We found the horse
MessageBox.Show(
$"Found horse {dt["Horse"]} (course={dt["Course"]} race={dt["Race"]} cloth={dt["Cloth"]} rating={dt["Rating"]} odds={dt["Odds"]}");
// TODO: Do something with the data row
dt["Rating"] = 5; // Possibly decimal.Parse(txtRating.Text); ?
// Apply updates
dt.AcceptChanges();
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句