我正在Windows窗体应用程序上工作。
它包含两种形式的MainForm-Test和childForm-Search
搜索表单具有dataGridView控件,其中包含SrNo,TypeNo,TestEngineer和Date之类的列。 测试表单包含文本框tb_SerialNo,tb_TypeNo,tb_TestEngineer和datTimePicker作为日期。
我的主要问题是,当我从datagridview中选择“行”时,我想要文本框中tb_SerialNo中该行的SrNo列值。所有人都一样。
我写了以下代码。但是它只给我tb_SerialNo中的SrNo值。但是我没有在相应的文本框中得到TypeNo,TestEnginer和日期值。我找不到我想念的东西。请帮助我解决此问题。预先感谢。
mainForm-测试代码
private void SearchToolStripMenuItem_Click(object sender, EventArgs e)
{
SearchTest Search = new SearchTest(dt);
Search.ShowDialog();
dt.DefaultView.RowFilter = "";
tb_SerialNo.Text = Search.SerialNo;
Search.typeNo = tb_TypeNo.Text;
Search.TestEngineer = tb_TestEngineer.Text;
Search.Date = dateTimePicker1.Text;
}
ChildForm -SearTest代码:
public partial class SearchTest : Form
{
public SearchTest(DataTable TestData)
{
InitializeComponent();
dataGridView1.DataSource = TestData;
this.dataGridView1.Sort(this.dataGridView1.Columns["SrNo"], ListSortDirection.Ascending);
}
private void btn_Search_Click(object sender, EventArgs e)
{
string str = dateTimePicker1.Text;
string str1 = dateTimePicker2.Text;
DateTime date = DateTime.ParseExact(str, "MM/dd/yyyy", CultureInfo.GetCultureInfo("en-GB"));
DateTime date1 = DateTime.ParseExact(str1, "MM/dd/yyyy", CultureInfo.GetCultureInfo("en-GB"));
string dtFilter = string.Format("[Date] >= '{0} ' AND [Date] <= '{1} '", date.ToString("MM/dd/yyyy"), date1.ToString("MM/dd/yyyy"));
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = dtFilter;
}
private void tb_SearchSrNo_TextChanged(object sender, EventArgs e)
{
try
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = String.IsNullOrEmpty(tb_SearchSrNo.Text) ?
"SrNo IS NOT NULL" :
String.Format("SrNo LIKE '{0}%' OR SrNo LIKE '{1}%' OR SrNo LIKE '{2}%'", tb_SearchSrNo.Text, tb_SearchSrNo.Text, tb_SearchSrNo.Text);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SearchTest_Load(object sender, EventArgs e)
{
groupBox1.Enabled = false;
groupBox3.Enabled = false;
cb_Filter.MouseWheel += new MouseEventHandler(cb_Filter_MouseWheel);
}
private void cb_Filter_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Filter.SelectedIndex == 0)
{
groupBox1.Enabled = true;
}
else
{
groupBox3.Enabled = true;
}
}
void cb_Filter_MouseWheel(object sender, MouseEventArgs e)
{
((HandledMouseEventArgs)e).Handled = true;
}
private string SrNo;
private string TypeNo;
private string TestEng;
private string date;
public string SerialNo
{
get { return SrNo; }
set { SrNo = value; }
}
public string typeNo
{
get { return TypeNo; }
set { TypeNo = value; }
}
public string TestEngineer
{
get { return TestEng; }
set { TestEng = value; }
}
public string Date
{
get { return date; }
set { date = value; }
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
SrNo = dataGridView1.CurrentRow.Cells["SrNo"].Value.ToString();
TypeNo = dataGridView1.CurrentRow.Cells["TypeNo"].Value.ToString();
TestEng = dataGridView1.CurrentRow.Cells["TestEngineer"].Value.ToString();
date = dataGridView1.CurrentRow.Cells["Date"].Value.ToString();
}
}
答案 0 :(得分:1)
您可以将属性定义更改为以下内容:
public string SomeProperty
{
get
{
string value = null;
if(BindingContext[dataGridView1.DataSource].Current !=null)
{
var r = ((DataRowView)BindingContext[dataGridView1.DataSource].Current).Row;
value = r.Field<string>("SomeDataColumn");
}
return value;
}
}
这样,SomeProperty
将始终从活动行中返回SomeDataColumn
的值。
答案 1 :(得分:0)
尝试改用SelectedRow。
SelectedRow和CurrentRow之间的区别:
这是示例代码:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
{
int index = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[index];
SrNo = selectedRow.Cells["SrNo"].Value.ToString();
TypeNo = selectedRowCells["TypeNo"].Value.ToString();
TestEng = selectedRowCells["TestEngineer"].Value.ToString();
date = selectedRowCells["Date"].Value.ToString();
}
}
答案 2 :(得分:0)
我找到了答案,问题已经解决。我在mainForm中发现了我的错误-测试代码。除序列号外,我以错误的方式将属性用于typeNo,TestEngineer和日期。这就是为什么我没有在各个文本框中获取值的原因。
更正后的MainForm-测试代码:
private void SearchToolStripMenuItem_Click(object sender, EventArgs e)
{
SearchTest Search = new SearchTest(dt);
Search.ShowDialog();
dt.DefaultView.RowFilter = "";
tb_SerialNo.Text = Search.SerialNo;
tb_TypeNo.Text = Search.typeNo;
tb_TestEngineer.Text = Search.TestEngineer;
dateTimePicker1.Text = Search.Date;
}
无需在搜索表单中进行任何更改。