[英]textbox value in c#
我刚刚将StudentID和StudentName更新到数据库中。
我使用以下代码来查看文本框中的值,但是当我想选择行时,它应该是ID行而不是StudentID。
我的意思是我希望看到StudentID的值,而不是ID行。
我不想使用ID行来查看StudentName。
我想在输入StudentID时看到StudentName。
sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
adapter = new SqlDataAdapter("select * from Entry",sql);
dt = new DataTable();
adapter.Fill(dt);
textBox1.Text = dt.Rows[3]["StudentName"].ToString();
如果studentID是表的主键,则使用:
DataRow row = dt.Rows.Find(studentID);
if (row != null)
textBox1.Text = row["StudentName"].ToString();
否则使用dt.Select
方法。 顺便说一下,将数据访问代码与UI代码混合起来并不是一个好主意
更新:你也可以使用LINQ
string name = (from row in dt.AsEnumerable()
where row.Field<int>("StudentID") == studentID
select row.Field<string>("StudenName"))
.Single();
更新:如果您输入学生ID并想获得学生姓名,那么您可以从数据库中检索学生姓名,将参数传递给sql命令:
private string GetStudentName(int studentID)
{
string connString = @"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
string query = "SELECT StudentName FROM Entry WHERE StudentID = @studentID";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID;
conn.Open();
return (string)cmd.ExecuteScalar();
}
}
考虑也只返回第一个条目(如果StudentID不是PK)并验证DbNull。
更新:如果您需要检索学生的几个属性,那么我创建了类Student:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Grade { get; set; }
}
并从数据阅读器填充其属性:
private Student GetStudent(int studentID)
{
string connString = @"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
string query = "SELECT * FROM Entry WHERE StudentID = @studentID";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (!reader.Read())
throw new Exception("Student not found");
return new Student()
{
Id = (int)reader["StudentID"],
Name = (string)reader["StudentName"],
Grade = (string)reader["Grade"]
};
}
}
然后,当您在文本框中输入学生ID时,从数据库中检索学生并在控件中显示其属性:
int studentID = Int32.Parse(idTextBox.Text);
Student student = GetStudent(studentID);
nameTextBox.Text = student.Name;
gradeTextBox.Text = student.Grade;
如果您要搜索一组学生,请显示名称列表
当您获得选择时,获取所选行的ID ...
你应该使用ComboBox / ListBox
现在你有一个像这样的清单
-Tomer Weinberg
-aliprogrammer
-some freek
当您查询myComboBox.SelectedValue时,您将获得该学生的ID,如果未选择任何ID,则为NULL。
编辑
把它放在带有标签和列表框的表格内(可以是组合框)
private DataTable dataTable1;
public Form1()
{
InitializeComponent();
dataTable1 = new DataTable("myTable");
dataTable1.Columns.Add("id", typeof (int));
dataTable1.Columns.Add("name", typeof(string));
dataTable1.Rows.Add(1, "Tomer");
dataTable1.Rows.Add(2, "Ali");
dataTable1.Rows.Add(3, "Some Other");
listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged);
listBox1.DataSource = dataTable1; // collection of Rows
listBox1.ValueMember = "id"; // what is the value of the row.
listBox1.DisplayMember = "name"; // what should be visible to user
listBox1.Refresh();
}
void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
label1.Text = string.Format("Selected: {0}", listBox1.SelectedValue);
}
祝你好运,
在我的旧帖子的评论之后,这是一个对话框示例
public class MySearchForm
{
public string SelectedSID { get; private set;}
// code to show a list of Students and StudentsIDs.
}
public class myMainForm
{
public void SearchButton_Click(object sender, EventArgs ea)
{
using(MySearchForm searchForm = new MySearchForm())
{
if(DialogResult.OK == searchForm.ShowDialog())
{
mySutdentIDTextBox.Text = searchForm.SelectedSID;
}
}
}
}
您可以在调用ShowDialog()之前使用构造函数自定义对话框并设置参数
您可以添加更多信息以从对话框中获取...
实际上,它有点像使用OpenFileDialog表单。 获取用户选定的文件
祝你好运,享受。
您还可以使用DataTable.Select方法。 您可以在其中传递过滤器表达式。 它返回DataRow数组
DataRow [] rowCollection= dt.Select("StudentID=" + <From Some Control> or method argument)
这是链接 http://msdn.microsoft.com/en-us/library/det4aw50.aspx
您可以这样使用Select:
DataTable dt = new DataTable("Test");
dt.Columns.Add("ID") ;
dt.Columns.Add("StudentID");
dt.Columns.Add("StudentName");
object[] rowVals = new object[3];
rowVals[0] = "1";
rowVals[1] = "ST-1";
rowVals[2] = "Kunal Uppal";
dt.Rows.Add(rowVals);
string studentID = "ST-1"; //this can come from a textbox.Text property
DataRow[] collection= dt.Select("StudentID=" + "'" + studentID + "'");
string studentName = Convert.ToString(collection[0]["StudentName"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.