使用不同的索引绑定相同的下拉列表 [英] Bind Same Dropdownlist with Different index
本文介绍了使用不同的索引绑定相同的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个动态gridview,其中每一行都有一个新的下拉列表,所以我想设置第一行索引的dropownlist为1,第二行索引为2,依此类推......
我的代码如下,请帮助。
i have a dynamic gridview in which each row have a new dropdownlist so i want to set dropsownlist for 1st row index will be 1, for 2nd row index will be 2 and so on...
my code is as follow please help.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FirstGridViewRow();
FirstGridViewRowoutput();
txtDate.Text = DateTime.Now.ToString("MM/dd/yyyy").Replace("-", "/");
AddNewRow();
AddNewRow();
AddNewRowoutput();
//grddtl.Rows[0].Cells[1].
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRow();
}
private void FirstGridViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Col1"] = string.Empty;
dr["Col2"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
grddtl.DataSource = dt;
grddtl.DataBind();
}
private void AddNewRow()
{
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++)
{
DropDownList TextBoxName = (DropDownList)grddtl.Rows[rowIndex].Cells[1].FindControl("ddlitem");
TextBox TextBoxQty =
(TextBox)grddtl.Rows[rowIndex].Cells[2].FindControl("txtqty");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grddtl.DataSource = dtCurrentTable;
grddtl.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList TextBoxName = (DropDownList)grddtl.Rows[rowIndex].Cells[1].FindControl("ddlitem");
TextBox TextBoxQty = (TextBox)grddtl.Rows[rowIndex].Cells[2].FindControl("txtqty");
TextBoxName.SelectedValue = dt.Rows[i]["Col1"].ToString();
TextBoxQty.Text = dt.Rows[i]["Col2"].ToString();
rowIndex++;
}
}
}
}
protected void grddtl_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTable"] = dt;
grddtl.DataSource = dt;
grddtl.DataBind();
for (int i = 0; i < grddtl.Rows.Count - 1; i++)
{
grddtl.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
}
SetPreviousData();
}
}
}
private void SetRowData()
{
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++)
{
DropDownList TextBoxName =(DropDownList)grddtl.Rows[rowIndex].Cells[1].FindControl("ddlitem");
TextBox TextBoxQty = (TextBox)grddtl.Rows[rowIndex].Cells[2].FindControl("txtqty");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
//grvStudentDetails.DataSource = dtCurrentTable;
//grvStudentDetails.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//SetPreviousData();
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
SetRowData();
SetRowDataoutput();
DataTable table = ViewState["CurrentTable"] as DataTable;
DataTable tableoutput = ViewState["CurrentTableoutput"] as DataTable;
if (table != null && tableoutput !=null)
{
conn.Open();
SqlCommand cmdhdr = new SqlCommand("insert into productionhdr(date,note) values ('" + txtDate.Text + "','" + txtNote.Text + "') ", conn);
SqlDataAdapter dahdr = new SqlDataAdapter(cmdhdr);
cmdhdr.ExecuteNonQuery();
SqlCommand getID = new SqlCommand("Select max(id) from productionhdr", conn);
SqlDataAdapter da = new SqlDataAdapter(getID);
DataSet ds = new DataSet();
da.Fill(ds);
//if (ds.Tables[0].Rows[0][0] == DBNull.Value)
//{
// maxrowid = 1;
// //int id = (int)ds.Tables[0].Rows[0][0];
//}
//else
//{
maxrowid = (int)ds.Tables[0].Rows[0][0];
//}
//if (maxrowid > 1)
// maxrowid = maxrowid + 1;
foreach (DataRow row in table.Rows)
{
string txtName = row.ItemArray[1] as string;
string txtQty = row.ItemArray[2] as string;
if (txtName != null || txtQty != null)
{
SqlCommand cmddtl = new SqlCommand("insert into productiondtl(pid,item,qty,type) values(" + maxrowid + ",'" + txtName + "'," + txtQty + ",'I')", conn);
SqlDataAdapter dadtl = new SqlDataAdapter(cmddtl);
//DataSet ds=new DataSet();
//da.Fill(ds);
cmddtl.ExecuteNonQuery();
}
}
foreach (DataRow row in tableoutput.Rows)
{
string txtName = row.ItemArray[1] as string;
string txtQty = row.ItemArray[2] as string;
if (txtName != null || txtQty != null)
{
SqlCommand cmddtloutput = new SqlCommand("insert into productiondtl(pid,item,qty,type) values(" + maxrowid + ",'" + txtName + "'," + txtQty + ",'O')", conn);
SqlDataAdapter dadtloutput = new SqlDataAdapter(cmddtloutput);
//DataSet ds=new DataSet();
//da.Fill(ds);
cmddtloutput.ExecuteNonQuery();
}
}
lblmsg.Text = "Data Saved Sucessfully...!!!!";
conn.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlItem = (e.Row.FindControl("ddlitem") as DropDownList);
conn.Open();
string query = "SELECT id,name FROM itemmst";
SqlCommand cmd = new SqlCommand(query,conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
ddlItem.DataSource = ds;
ddlItem.DataTextField = "name";
ddlItem.DataValueField = "id";
ddlItem.DataBind();
//Add Default Item in the DropDownList
ddlItem.Items.Insert(0, new ListItem("Select Item"));
conn.Close();
//Select the Country of Customer in DropDownList
}
}
//for output
protected void ButtonAddoutput_Click(object sender, EventArgs e)
{
AddNewRowoutput();
}
private void FirstGridViewRowoutput()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumberoutput", typeof(string)));
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dr = dt.NewRow();
dr["RowNumberoutput"] = 1;
dr["Col1"] = string.Empty;
dr["Col2"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTableoutput"] = dt;
grddtloutput.DataSource = dt;
grddtloutput.DataBind();
}
private void AddNewRowoutput()
{
int rowIndex = 0;
if (ViewState["CurrentTableoutput"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableoutput"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList TextBoxName =
(DropDownList)grddtloutput.Rows[rowIndex].Cells[1].FindControl("ddlitemoutput");
TextBox TextBoxQty =
(TextBox)grddtloutput.Rows[rowIndex].Cells[2].FindControl("txtqtyoutput");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumberoutput"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTableoutput"] = dtCurrentTable;
grddtloutput.DataSource = dtCurrentTable;
grddtloutput.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousDataoutput();
}
private void SetPreviousDataoutput()
{
int rowIndex = 0;
if (ViewState["CurrentTableoutput"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTableoutput"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList TextBoxName = (DropDownList)grddtloutput.Rows[rowIndex].Cells[1].FindControl("ddlitemoutput");
TextBox TextBoxQty = (TextBox)grddtloutput.Rows[rowIndex].Cells[2].FindControl("txtqtyoutput");
TextBoxName.SelectedValue = dt.Rows[i]["Col1"].ToString();
TextBoxQty.Text = dt.Rows[i]["Col2"].ToString();
rowIndex++;
}
}
}
}
protected void grddtloutput_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowDataoutput();
if (ViewState["CurrentTableoutput"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTableoutput"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTableoutput"] = dt;
grddtloutput.DataSource = dt;
grddtloutput.DataBind();
for (int i = 0; i < grddtloutput.Rows.Count - 1; i++)
{
grddtloutput.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
}
SetPreviousDataoutput();
}
}
}
private void SetRowDataoutput()
{
int rowIndex = 0;
if (ViewState["CurrentTableoutput"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableoutput"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList TextBoxName = (DropDownList)grddtloutput.Rows[rowIndex].Cells[1].FindControl("ddlitemoutput");
TextBox TextBoxQty = (TextBox)grddtloutput.Rows[rowIndex].Cells[2].FindControl("txtqtyoutput");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumberoutput"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;
rowIndex++;
}
ViewState["CurrentTableoutput"] = dtCurrentTable;
//grvStudentDetails.DataSource = dtCurrentTable;
//grvStudentDetails.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//SetPreviousData();
}
protected void OnRowDataBoundoutput(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlItem = (e.Row.FindControl("ddlitemoutput") as DropDownList);
conn.Open();
string query = "SELECT id,name FROM itemmst";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlItem.DataSource = ds;
ddlItem.DataTextField = "name";
ddlItem.DataValueField = "id";
ddlItem.DataBind();
//Add Default Item in the DropDownList
ddlItem.Items.Insert(0, new ListItem("Select Item"));
conn.Close();
//Select the Country of Customer in DropDownList
}
}
推荐答案
replace youronRowdatabound
event with below event
may it will help you
replace youronRowdatabound
event with below event
may it will help you
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
//Find the DropDownList in the Row
DropDownList ddlItem = (e.Row.FindControl("ddlitem") as DropDownList);
conn.Open();
string query = "SELECT id,name FROM itemmst";
SqlCommand cmd = new SqlCommand(query,conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
ddlItem.DataSource = ds;
ddlItem.DataTextField = "name";
ddlItem.DataValueField = "id";
ddlItem.DataBind();
//Add Default Item in the DropDownList
ddlItem.Items.Insert(0, new ListItem("Select Item"));
conn.Close();
ddlItem.SelectedIndex=index;
//Select the Country of Customer in DropDownList
}
}
这篇关于使用不同的索引绑定相同的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文