c# - 如何通过datagrid鼠标双击在新窗口中填充组合框
问题描述
public partial class DataGrid_HBD : UserControl
{
public DataGrid_HBD()
{
InitializeComponent();
// 2 Seconds Timer before connecting to the Database.
// This improves UI rendering on button click
DataGrid_Data();
}
/// <summary>
/// Loading Data Grid
/// </summary>
public void DataGrid_Data()
{
// 2 second delay before loading DataGrid
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(5) };
timer.Start();
timer.Tick += (sender, args) =>
{
timer.Stop();
// Attempt to connect to SQL Server database and populate DataGrid with database tables.
try
{
string connectionString = ("Data Source=\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT [hb_Disputes].[DSP_ID], [hb_disputes].[ACCOUNT], [Users].[TX_EMPLOYEE], [hb_CsrNames].[NM_USER], [hb_disputes].[CUST_NAME],[hb_disputes].[PREM_ADDR], [hb_status].[Status], [hb_disputes].[OPENED], [hb_disputes].[DEADLINE], [hb_disputes].[DATERSLVD], [hb_rpttype].[ReportType], [hb_ratetype].[RateType], [hb_Disputes].[FR_DT_FIRSTREV], [hb_Disputes].[FR_TS_LATESTUPD], [hb_Disputes].[COMMENT], [hb_Disputes].[FR_DSP_CLSF], [hb_Disputes].[FR_CUST_CNTCT], [hb_Disputes].[FR_WRK_REQ], [hb_Disputes].[FR_OPN_ERR], [hb_Disputes].[FR_SO_TP], [hb_Disputes].[FR_SO_DTLS], [hb_Disputes].[FR_SO_DT_WNTD], [hb_Disputes].[FR_SO_ISSD_BY], [hb_Disputes].[FR_CMMNT] FROM [hb_disputes]" +
" LEFT JOIN [Users] ON [hb_disputes].[ASSGNTO] = [Users].[KY_USER_ID] LEFT JOIN [hb_CsrNames] ON [hb_disputes].[WFMUSER] = [hb_CsrNames].[KY_USER_ID] LEFT JOIN [hb_status] ON [hb_disputes].[STATUS] = [hb_status].[STSID] LEFT JOIN [hb_rpttype] ON [hb_disputes].[RPTTYPE] = [hb_rpttype].[RPTID] LEFT JOIN [hb_ratetype] ON [hb_disputes].[REV_CLS] = [hb_ratetype].[RTID]", connection);
connection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connection.Close();
dtGrid.DataContext = dt;
}
catch
{
MessageBox.Show("Database connection is not available at this time. Please contact your database administrator ");
}
};
}
private void dtGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// User double clicks on DataGrid Row
// Open new Window
// Populate selected textboxes with selected datarow
DataGrid gd = (DataGrid)sender;
DataRowView row_selected = gd.SelectedItem as DataRowView;
var windowToOpen = new Window1();
if(gd !=null )
{
// Textboxes
windowToOpen.txt_RowRecrd.Text = row_selected["DSP_ID"].ToString();
windowToOpen.txt_acctnumber.Text = row_selected["ACCOUNT"].ToString();
windowToOpen.txt_analyst.Text = row_selected["TX_EMPLOYEE"].ToString();
windowToOpen.txt_custname.Text = row_selected["CUST_NAME"].ToString();
windowToOpen.txt_address.Text = row_selected["PREM_ADDR"].ToString();
windowToOpen.txt_Status.Text = row_selected["Status"].ToString();
windowToOpen.txt_opened.Text = row_selected["OPENED"].ToString();
windowToOpen.txt_deadline.Text = row_selected["DEADLINE"].ToString();
windowToOpen.txt_DateResolved.Text = row_selected["DATERSLVD"].ToString();
windowToOpen.txt_revcls.Text = row_selected["RateType"].ToString();
windowToOpen.txt_WFMissuedBy.Text = row_selected["NM_USER"].ToString();
windowToOpen.txt_firstreview.Text = row_selected["FR_DT_FIRSTREV"].ToString();
windowToOpen.txt_Latestupdate.Text = row_selected["FR_TS_LATESTUPD"].ToString();
windowToOpen.txt_reviewNotes.Text = row_selected["FR_CMMNT"].ToString();
windowToOpen.txt_ResolutionNotes.Text = row_selected["COMMENT"].ToString();
// Comboboxes
windowToOpen.cmb_UtilityRptTyp.SelectedItem = row_selected["ReportType"].ToString();
windowToOpen.Show();
}
else
{
return;
}
}
当用户双击数据网格中的一行时,它会打开一个新窗口并使用选定的单元格填充文本框。但是,它不适用于组合框。我附上了正在填充信息的新窗口 (Window1) 的图像。该图显示了组合框背后的代码,其中包含来自 SQL Server 数据库的填充表。
解决方案
首先,您必须设置Datasource
一个cmb_UtilityRptTyp
可用的报告类型列表,如下所示:
// Define ReportType Class
class ReportType {
public int ID { get; set; }
public string Title { get; set; }
public ReportType(int id, string title)
{
ID = id;
Title = title;
}
}
然后DataSource
在第一行设置dtGrid_MouseDoubleClick
:
ReportType[] list = new ReportType[] {
new ReportType(1, "Type 1"),
new ReportType(2, "Type 2"),
};
windowToOpen.cmb_UtilityRptTyp.DataSource = list;
windowToOpen.cmb_UtilityRptTyp.DisplayMember = "Title";
windowToOpen.cmb_UtilityRptTyp.ValueMember = "ID";
之后,您必须使用inSelectedText
而不是Text
in ComboBox
,如下所示:
windowToOpen.cmb_UtilityRptTyp.SelectedText = row_selected["RPTTYPE"].ToString();
此外,您可以使用, 来查找您可能喜欢SelectedIndex
的项目索引IndexOf
string rptType = row_selected["RPTTYPE"].ToString();
int index = windowToOpen.cmb_UtilityRptTyp.Items.IndexOf(rptType );
windowToOpen.cmb_UtilityRptTyp.SelectedIndex = index;
或使用FindStringExact
string rptType = row_selected["RPTTYPE"].ToString();
int index = windowToOpen.cmb_UtilityRptTyp.FindStringExact(rptType );
windowToOpen.cmb_UtilityRptTyp.SelectedIndex = index;
推荐阅读
- bash - Bash脚本在动态变量上更改部分文件夹路径
- python - 尝试将 pandas 数据框导出为 excel 文件时出错
- javascript - 如何使用 NodeJS 和 Yahoo! 发出 POST 请求 梦幻体育API
- c++ - ',' 标记之前应为 ')' - SFML 创建 RectangleShape
- python - 使用 N 个工作人员执行多处理任务列表的最佳方法?
- algorithm - 水壶问题GCD解决方法解释
- javascript - Puppeteer 无法在特定的输入字段中写入
- java - 在 JMH 中的每个基准测试之后恢复 Java 列表状态
- react-native - ReactNative:使用 Axios 将图像文件上传到服务器不起作用
- c++ - macosx 上信号处理程序中的 rip 寄存器值