我在使用SQL语句时遇到问题,无法将我的dropdownlist select中的值传递给gridview。我测试了我的SQL语句Serve Management Studio,具有特定的日期,并且它可以工作,但它不能使用我的Dropdownlist中的select值。我如何将值传递给Gridview?谢谢你的帮助,我是asp.net世界的新生。
public void RefreshDay()
{
SqlConnection conn = new SqlConnection(@"data source =.\sqlexpress; integrated security = true; database = DBdentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT Distinct patient.patientID, day from patient, patientreservation where patient.patientID = patientreservation.patientID";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
DropDownListDay.DataSource = dt;
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "patientID";
DropDownListDay.DataBind();
DropDownListDay.Items.Insert(0, "Select Day");
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
protected void DropDownListDay_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListDay.SelectedIndex != 0)
{
SqlConnection conn = new SqlConnection(@"data source =.\sqlexpress; integrated security = true; database = DbDentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE patientreservation.day = " + DropDownListDay.SelectedValue + "";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
GridViewDay.DataSource = dt;
GridViewDay.DataBind();
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
else
{
LabelDay.Text = "You choose None:";
}
}
}
}
答案 0 :(得分:0)
您的下拉列表的配置是
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "day";
SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE **patientreservation.day** = " + DropDownListDay.**SelectedValue** + "";
但是如果要使用当前选定的值进行过滤,则需要天作为值
WHERE **patientreservation.day** = " + DropDownListDay.**SelectedText** + "";
或者您可以尝试使用<DataGrid x:Name="dg">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Place}"
Value="true">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
答案 1 :(得分:0)
你可以简单地使用它:
WHERE patientreservation.day = '" + DropDownListDay.Text.ToString() + "'";
答案 2 :(得分:0)
感谢您的帮助。我尝试了一切
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "day"
帮助。我的gridview也遇到了一些问题。我加上删除它并制作一个新的,不知怎的帮助。
答案 3 :(得分:-1)
下拉列表未触发此事件。尝试将此AutoPostBack =“true”放在asp下拉列表标记中。