使用数据表单数据库在DataGridView中填充文本框 [英] Fill textbox inside a DataGridView with data form database
问题描述
你好兄弟
i希望用数据表单数据库填充DataGridView中的文本框依赖于DataGridView里面的组合框中的选定值
这是填写cobobox的代码
hello brothers
i want to Fill textbox inside a DataGridView with data form database depended on selected value from combobox inside the DataGridView
this is the code for filling the cobobox
private void create_report_Load(object sender, EventArgs e)
{
SqlConnection objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
SqlCommand objCmd = new SqlCommand("SELECT * FROM [testing_name];", objCon);
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
DataTable dt = new DataTable();
objDA.Fill(dt);
Test_Name.DataSource = dt;
Test_Name.DisplayMember = "test_name";
Test_Name.ValueMember = "test_defualt_id";
}
我尝试使用此代码填写文本框< br $>
and i try this code for filling the textbox
private void Test_Name_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cnz = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
SqlCommand cmdzs = new SqlCommand("SELECT * FROM [test_normal] WHERE test_id='" + Test_Name.Selected + "' ;", cnz);
cmdzs.CommandType = CommandType.Text;
cmdzs.Connection.Open();
SqlDataReader dr = cmdzs.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
//test_result.Text = dr["patient_gender"].ToString();
//reference_rang.Text = dr["patient_birth_date"].ToString();
}
}
cmdzs.Connection.Close();
}
但是给我一个错误,没有属性到DataGridView里面的文本框
此图片可以解释更多
http://img545.imageshack.us/img545/3850/d884.png [ ^ ]
#new_edit
i尝试此代码
but give me error with no property to the textbox that inside the DataGridView
this image can explain more
http://img545.imageshack.us/img545/3850/d884.png[^]
#new_edit
i try this code
public create_report()
{
InitializeComponent();
define_gridview_columns();
add_rows();
}
public void define_gridview_columns()
{
DataGridViewComboBoxColumn test_name = new DataGridViewComboBoxColumn();
test_name.HeaderText = "Test Name";
test_name.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
SqlConnection objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
SqlCommand objCmd = new SqlCommand("SELECT dbo.testing_name.test_name,dbo.testing_name.test_defualt_id FROM dbo.testing_name ;", objCon);
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
DataTable dt = new DataTable();
objCon.Open();
objDA.Fill(dt);
test_name.DataSource = dt;
test_name.DisplayMember = "test_name";
test_name.ValueMember = "test_defualt_id";
objCon.Close();
DataGridViewTextBoxColumn test_unit = new DataGridViewTextBoxColumn();
test_unit.HeaderText = "Normail Unit";
test_unit.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
DataGridViewTextBoxColumn ref_rang = new DataGridViewTextBoxColumn();
ref_rang.HeaderText = "Reference Rang";
ref_rang.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
DataGridViewTextBoxColumn new_result = new DataGridViewTextBoxColumn();
new_result.HeaderText = "New Result";
new_result.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.Columns.Add(test_name);
dataGridView1.Columns.Add(test_unit);
dataGridView1.Columns.Add(ref_rang);
dataGridView1.Columns.Add(new_result);
}
public void add_rows()
{
for (int i = 0; i < 10; i++)
{
dataGridView1.Rows.Add();
}
}
这创造了我需要的控件
我用我需要的数据填写cobobox
现在我需要的是从数据库获取数据到创建的数据文本框
i使用此代码,但datagridview textbos不存在属性
this create the controll that i need
and i fill the cobobox with the data that i need
now what i need is get data from the DB to the created textbox
i use this code but the property not existed for the datagridview textbos
//SqlConnection cnz = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
//SqlCommand cmdzs = new SqlCommand("SELECT dbo.test_normal.test_normail_unit,dbo.test_normal.test_normal_reference_rang_id FROM dbo.test_normal WHERE dbo.test_normal.test_normal_id='" + test_name.ValueMember + "' ;", cnz);
//cmdzs.CommandType = CommandType.Text;
//cmdzs.Connection.Open();
//SqlDataReader dr = cmdzs.ExecuteReader();
//if (dr.HasRows)
//{
// while (dr.Read())
// {
// test_unit.Text = dr["patient_gender"].ToString();
// ref_rang.Text = dr["patient_birth_date"].ToString();
// }
//}
//cmdzs.Connection.Close();
推荐答案
您好,
您可以使用DatagridView EditingControlShowing事件来实现此目的。 />
Hello ,
you can use DatagridView EditingControlShowing event to achieve this .
private void dataGridViewSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged += new EventHandler(selectionchange);
//here 'SelectedIndexChanged' event is used
}
}
然后写'selectionchange'方法
and then write 'selectionchange' method
void selectionchange(object sender, EventArgs e)
{
try
{
ComboBox cb = (ComboBox)sender;
//write here the code to get the value according to combobox selection change
}
catch { }
}
谢谢
animesh
thanks
animesh
我认为你不需要那么多的工作,
只需使用cellendedit 事件或 CellValueChanged [ ^ ]事件
有关详细信息,请参阅cellvaluechanged事件中给出的示例
i think you don't need that much of work there,
just use the cellendedit event or CellValueChanged[^] Event
for further details see the example given in cellvaluechanged event
这篇关于使用数据表单数据库在DataGridView中填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!