我的表单显示空GridView以及添加新按钮。我在第1列中有一个下拉列表,它从数据库中填充。根据从下拉列表中选择的内容,我查询SQL数据库并填充第2列和第3列。我无法将下拉列表的值写入数据表并添加新行。当GridView只有文本框时,它运行良好,但添加数据绑定下拉列表使它很复杂。这是我的代码:
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Item Number">
<ItemTemplate>
<asp:DropDownList ID="ddlItemNumber" runat="server" DataSourceID="sdsSubjects" DataTextField="item_num" DataValueField="item_num" OnSelectedIndexChanged="Index_Changed" AutoPostBack="true" OnDataBound="addDefaultValue_DataBound">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsSubjects" runat="server" connectionstring="<%$ ConnectionStrings:test%>"
SelectCommand="SELECT item_num FROM tblItemList"></asp:SqlDataSource>
<asp:RequiredFieldValidator ID="reqItemNumber" ValidationGroup="ValgrpCust" ControlToValidate="ddlItemNumber" runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
这是我的代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void addDefaultValue_DataBound(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
if (list != null)
{
list.Items.Insert(0, "--Select One--");
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Item Number", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("Unit", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
dr = dt.NewRow();
dr["Item Number"] = string.Empty;
dr["Description"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Quantity"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
DropDownList ddlList1 = (DropDownList)Gridview1.Rows[rowIndex].FindControl("ddlItemNumber");
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
//drCurrentRow["Ro wNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Item Number"] = ddlList1.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Description"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Unit"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Quantity"] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddlList1 = (DropDownList)Gridview1.Rows[rowIndex].FindControl("ddlItemNumber");
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].FindControl("TextBox3");
ddlList1.SelectedItem.Text = dt.Rows[i]["Item Number"].ToString();
box1.Text = dt.Rows[i]["Description"].ToString();
box2.Text = dt.Rows[i]["Unit"].ToString();
box3.Text = dt.Rows[i]["Quantity"].ToString();
rowIndex++;
}
}
}
}
protected void Index_Changed(Object sender, EventArgs e) {
DropDownList ddlItemNumber = (DropDownList)sender;
GridViewRow grdrDropDownRow = ((GridViewRow)ddlItemNumber.Parent.Parent);
int index = grdrDropDownRow.RowIndex;
//lblMessage.Text = ddlItemNumber.SelectedItem.Text;
DataSet myDataSet = GetData("SELECT description, unit FROM tblItemList WHERE item_num='" + ddlItemNumber.SelectedItem.Text.Trim
() + "'");
grdrDropDownRow.Cells[1].Text = myDataSet.Tables[0].Rows[0]["description"].ToString();
grdrDropDownRow.Cells[2].Text = myDataSet.Tables[0].Rows[0]["unit"].ToString();
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["Item Number"] = grdrDropDownRow.Cells[1].Text;
dt.Rows[i]["Description"] = grdrDropDownRow.Cells[2].Text;
dt.Rows[i]["Unit"] = grdrDropDownRow.Cells[3].Text;
dt.Rows[i]["Quantity"] = grdrDropDownRow.Cells[4].Text;
}
}
SetPreviousData();
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
感谢您提供任何线索和时间。
乔