如何使用Asp.net将数据库中的值放入组合框

如何使用Asp.net将数据库中的值放入组合框,asp.net,visual-studio,Asp.net,Visual Studio,我想把学生名单放到组合框中,在组合框选择的基础上,我将把他们的相关记录放到另一个网格中。请指导我完成此任务。您可以按如下方式绑定DropDownList DropDownList1.DataSource=GetStudentDataSet(); DropDownList1.DataTextField="StudentName"; DropDownList1.DataValueField="StudentID"; DropDownList1.DataBind(); void DropDownLi

我想把学生名单放到组合框中,在组合框选择的基础上,我将把他们的相关记录放到另一个网格中。请指导我完成此任务。

您可以按如下方式绑定DropDownList

DropDownList1.DataSource=GetStudentDataSet();
DropDownList1.DataTextField="StudentName";
DropDownList1.DataValueField="StudentID";
DropDownList1.DataBind();
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Fecth selected student id 
         int studentId = Convert.ToInt32(DropDownList1.SelectedValue);
        //Bind Grdivew
    }
将GridView绑定代码放在DropDownList的选定更改事件上,如下所示

DropDownList1.DataSource=GetStudentDataSet();
DropDownList1.DataTextField="StudentName";
DropDownList1.DataValueField="StudentID";
DropDownList1.DataBind();
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Fecth selected student id 
         int studentId = Convert.ToInt32(DropDownList1.SelectedValue);
        //Bind Grdivew
    }

您可以如下所示绑定DropDownList

DropDownList1.DataSource=GetStudentDataSet();
DropDownList1.DataTextField="StudentName";
DropDownList1.DataValueField="StudentID";
DropDownList1.DataBind();
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Fecth selected student id 
         int studentId = Convert.ToInt32(DropDownList1.SelectedValue);
        //Bind Grdivew
    }
将GridView绑定代码放在DropDownList的选定更改事件上,如下所示

DropDownList1.DataSource=GetStudentDataSet();
DropDownList1.DataTextField="StudentName";
DropDownList1.DataValueField="StudentID";
DropDownList1.DataBind();
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Fecth selected student id 
         int studentId = Convert.ToInt32(DropDownList1.SelectedValue);
        //Bind Grdivew
    }

你已经有什么了?密码?数据库表的结构?一些ORM?你已经有了什么?密码?数据库表的结构?一些ORM?我只有数据库,我只想从下拉列表中的student表中获取名称,然后在获取名称后,在更改下拉列表中,我想获取与所选StudentStudentName=Convert.ToInt32(ddstd.SelectedValue)相关的特定记录;它给出FormatException,输入字符串的格式不正确。@Touseef:如果要获取学生姓名,则无需转换为int。只需在字符串变量中收集所选值,如string studentname=DropDownList1.SelectedValue;我只有数据库,我只想从下拉列表中的student表中获取名称,然后在获取名称后,在更改下拉列表中,我将获取与所选StudentStudentName=Convert.ToInt32(ddstd.SelectedValue)相关的特定记录;它给出FormatException,输入字符串的格式不正确。@Touseef:如果要获取学生姓名,则无需转换为int。只需在字符串变量中收集所选值,如string studentname=DropDownList1.SelectedValue;