C# 我只是试着用学校的名字搜索结果,但是网格视图没有显示任何东西

C# 我只是试着用学校的名字搜索结果,但是网格视图没有显示任何东西,c#,asp.net,C#,Asp.net,这是我的代码…我只是尝试按学校名称搜索结果。但是网格视图没有显示任何内容。我的代码是 public void gridfill() { markSp spMark = new markSp(); DataTable dtbl = new DataTable(); dtbl = spMark.markViewAll(); gvResult.DataSource = dtbl; dtbl = spMark.markViewBySchool(txtSchoolN

这是我的代码…我只是尝试按学校名称搜索结果。但是网格视图没有显示任何内容。我的代码是

public void gridfill()
{
    markSp spMark = new markSp();
    DataTable dtbl = new DataTable();
    dtbl = spMark.markViewAll();
    gvResult.DataSource = dtbl;
    dtbl = spMark.markViewBySchool(txtSchoolName.Text);
    gvResult.DataSource = dtbl;    
}
protected void Button1_Click(object sender, EventArgs e)
{
    gridfill();
}
public DataTable markViewAll()
{
    DataTable dtbl = new DataTable();
    SqlDataAdapter sqlda = new SqlDataAdapter("markViewAll", sqlcon);
    sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
    sqlda.Fill(dtbl);
    return dtbl;
}
public DataTable markViewBySchool(string viewBySchool)
{
    DataTable dtbClass = new DataTable();
    SqlDataAdapter sqlda = new SqlDataAdapter("markViewBySchool",sqlcon);
    sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
    sqlda.SelectCommand.Parameters.Add("@schoolName", SqlDbType.VarChar).Value = viewBySchool;
    sqlda.Fill(dtbClass);
    return dtbClass;
}

由于已使用ASP.Net进行标记,因此需要调用
DataBind

gvResult.DataBind();
所以你的方法是:

public void gridfill()
{
    markSp spMark = new markSp();
    DataTable dtbl = new DataTable();
    dtbl = spMark.markViewAll(); //you are not using this anywhere
    gvResult.DataSource = dtbl;  // so you can get rid of these two lines
    dtbl = spMark.markViewBySchool(txtSchoolName.Text);
    gvResult.DataSource = dtbl;    
    gvResult.DataBind(); // This is missing
}

您应该看到:

因为您已经使用ASP.Net进行了标记,所以需要调用
DataBind

gvResult.DataBind();
所以你的方法是:

public void gridfill()
{
    markSp spMark = new markSp();
    DataTable dtbl = new DataTable();
    dtbl = spMark.markViewAll(); //you are not using this anywhere
    gvResult.DataSource = dtbl;  // so you can get rid of these two lines
    dtbl = spMark.markViewBySchool(txtSchoolName.Text);
    gvResult.DataSource = dtbl;    
    gvResult.DataBind(); // This is missing
}

您应该看到:

如下更改,代码中几乎没有问题,为什么要两次数据绑定到同一个gridview?即使数据绑定两次,也只显示上次设置的内容。我删除了几行代码,而您也忘记调用
gvResult.DataBind()

public void gridfill()
{
    markSp spMark = new markSp();
    gvResult.DataSource = spMark.markViewBySchool(txtSchoolName.Text);
    gvResult.DataBind();  
}

更改如下,代码中几乎没有问题,为什么两次数据绑定到同一个gridview?即使数据绑定两次,也只显示上次设置的内容。我删除了几行代码,而您也忘记调用
gvResult.DataBind()

public void gridfill()
{
    markSp spMark = new markSp();
    gvResult.DataSource = spMark.markViewBySchool(txtSchoolName.Text);
    gvResult.DataBind();  
}

@jasel,不客气,我猜你来自WinForm背景,因为这应该在那里起作用。:)请注意,设置datatable和设置datasource两次有点。。。冗余的OP在本例中甚至不需要datatable变量,也不需要调用markViewAll()。@J.Steen,你说得对,实际上只是指出了核心原因,只是在answer@habib是的,是的,我很困惑,事实上,我知道我是通过winform,bt nw oly进入asp的,那是y,,,,@jasel,删除答案中注释的那两行,如果您没有使用它们,@jasel,不客气,我猜您来自WinForm后台,因为这应该在那里起作用。:)请注意,设置datatable和设置datasource两次有点。。。冗余的OP在本例中甚至不需要datatable变量,也不需要调用markViewAll()。@J.Steen,你说得对,实际上只是指出了核心原因,只是在answer@habib是的,是的,我很困惑,事实上,我知道我是通过winform,bt nw oly进入asp的,那是y,,,,@jasel,删除答案中注释的那两行,如果您没有使用它们,