-5

我有一个 Windows 窗体应用程序。我有一个函数,它使用 Linq to SQL 来查找我想要的记录,但我不知道如何在我的 button.click 事件中调用此函数,以便单击函数将启动我的其他函数。

此外,在我的 button.click 事件中,我需要将 textBox1.Text 值设置为 sql 语句,但如何做到这一点?

更新 - 现在修复了解决方案,这是完整的代码:

public partial class Form1 : Form
{
    LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Student student = new Student();
        string city = textBox1.Text;

        searchCity(student, city);
    }

    public Exception searchCity(Student student, string searchCity)
    {
        try
        {
            if (string.IsNullOrEmpty(searchCity))
            {
                MessageBox.Show("Please enter a value");
            }
            else
            {
                var City = from stud in linqStud.Students
                           where stud.City == searchCity
                           select stud;

                dataGridView1.DataSource = City;

            }
            return null;
        }
        catch (Exception ex)
        {
            return ex;
        }

谢谢,

缺口

4

1 回答 1

0

您可能不想从该方法返回异常。它可以改用void返回类型。例如:

private void button1_Click(object sender, EventArgs e)
{
    Student student = new Student();//set up student obj...
    string city = "Foo";

    searchCity(student, city);
}

public void searchCity(Student student, string searchCity)
{
    //Do your search...
    //set the data source of your gridview

    dataGridView1.DataSource = City;
    dataGridView1.DataBind(); //Make sure you call databind
}
于 2013-07-04T15:17:09.310 回答