[英]How to merge more than two tables in Single Gridview using asp.net? using .merge method
[英]how to put more control in each single cell of the editable gridview using asp.net
我在我的Web應用程序中使用網格視圖控件。我的網格視圖看起來像這樣。
單擊“添加”按鈕時,我得到的結果如下所示。
但我的預期結果就是這個......我的錯誤在哪里? 我該怎么辦?
這是我的編碼..
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("S.No", typeof(string)));
dt.Columns.Add(new DataColumn("Article No", typeof(string)));
dt.Columns.Add(new DataColumn("Item Description", typeof(string)));
dt.Columns.Add(new DataColumn("Order Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("UOM", typeof(string)));
dt.Columns.Add(new DataColumn("Rate", typeof(string)));
dt.Columns.Add(new DataColumn("Currency", typeof(string)));
dt.Columns.Add(new DataColumn("Amount", typeof(string)));
DataRow dr = dt.NewRow();
dr["S.No"] = 1;
dr["Article No"] = string.Empty;
dr["Item Description"] = string.Empty;
dr["Order Quantity"] = 0;
dr["UOM"] = string.Empty;
dr["Rate"] = 0;
dr["Currency"] = string.Empty;
dr["Amount"] = 0;
dt.Rows.Add(dr);
ViewState["currenttable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList box2 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("DropDownList1");
box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
box2.DataTextField = "Port_Number";
box2.DataValueField = "Description";
box2.DataBind();
box2.Items.Insert(0, " ");
DropDownList box12 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList3");
box12.DataSource = SIMBL.ShowUnitItemInfo();
box12.DataTextField = "Name";
box12.DataValueField = "Symbol";
box12.DataBind();
box12.Items.Insert(0, " ");
DropDownList box13 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList4");
box13.DataSource = SIMBL.ShowUnitItemInfo();
box13.DataTextField = "Name";
box13.DataValueField = "Symbol";
box13.DataBind();
box13.Items.Insert(0, " ");
DropDownList box14 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList5");
box14.DataSource = SIMBL.ShowUnitItemInfo();
box14.DataTextField = "Name";
box14.DataValueField = "Symbol";
box14.DataBind();
box14.Items.Insert(0, " ");
}
}
public void addnewrow()
{
int rowIndex = 0;
if (ViewState["currenttable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["currenttable"];
DataRow drCurrentRow = null;
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("TextBox6");
DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox8");
TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox9");
TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox10");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox11");
Label box7 = (Label)GridView1.Rows[rowIndex].Cells[3].FindControl("Label6");
TextBox box8 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox13");
TextBox box9 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox14");
TextBox box10 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox15");
Label box11 = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("Label7");
DropDownList box12 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList3");
DropDownList box13 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList4");
DropDownList box14 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList5");
Label box15 = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label8");
TextBox box16 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox17");
TextBox box17 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox18");
TextBox box18 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox19");
Label box19 = (Label)GridView1.Rows[rowIndex].Cells[6].FindControl("Label9");
DropDownList box20 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList7");
DropDownList box21 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList8");
DropDownList box22 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList9");
Label box23 = (Label)GridView1.Rows[rowIndex].Cells[7].FindControl("Label0");
TextBox box24 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox21");
TextBox box25 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox22");
TextBox box26 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox23");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["S.No"] = i + 1;
dtCurrentTable.Rows[i - 1]["Article No"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box5.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box6.Text;
dtCurrentTable.Rows[i - 1]["Order Quantity"] = box8.Text;
dtCurrentTable.Rows[i - 1]["Order Quantity"] = box9.Text;
dtCurrentTable.Rows[i - 1]["Order Quantity"] = box10.Text;
dtCurrentTable.Rows[i - 1]["UOM"] = box12.Text;
dtCurrentTable.Rows[i - 1]["UOM"] = box13.Text;
dtCurrentTable.Rows[i - 1]["UOM"] = box14.Text;
dtCurrentTable.Rows[i - 1]["Rate"] = box16.Text;
dtCurrentTable.Rows[i - 1]["Rate"] = box17.Text;
dtCurrentTable.Rows[i - 1]["Rate"] = box18.Text;
dtCurrentTable.Rows[i - 1]["Currency"] = box20.Text;
dtCurrentTable.Rows[i - 1]["Currency"] = box21.Text;
dtCurrentTable.Rows[i - 1]["Currency"] = box22.Text;
dtCurrentTable.Rows[i - 1]["Amount"] = box24.Text;
dtCurrentTable.Rows[i - 1]["Amount"] = box25.Text;
dtCurrentTable.Rows[i - 1]["Amount"] = box26.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["currenttable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList box2 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("DropDownList1");
box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
box2.DataTextField = "Port_Number";
box2.DataValueField = "Description";
box2.DataBind();
box2.Items.Insert(0, " ");
}
}
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++)
{
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("TextBox6");
DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox8");
TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox9");
TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox10");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox11");
Label box7 = (Label)GridView1.Rows[rowIndex].Cells[3].FindControl("Label6");
TextBox box8 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox13");
TextBox box9 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox14");
TextBox box10 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox15");
Label box11 = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("Label7");
DropDownList box12 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList3");
DropDownList box13 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList4");
DropDownList box14 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList5");
Label box15 = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label8");
TextBox box16 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox17");
TextBox box17 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox18");
TextBox box18 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox19");
Label box19 = (Label)GridView1.Rows[rowIndex].Cells[6].FindControl("Label9");
DropDownList box20 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList7");
DropDownList box21 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList8");
DropDownList box22 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList9");
Label box23 = (Label)GridView1.Rows[rowIndex].Cells[7].FindControl("Label0");
TextBox box24 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox21");
TextBox box25 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox22");
TextBox box26 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox23");
box1.Text = dt.Rows[i]["S.No"].ToString();
box2.Text = dt.Rows[i]["Article No"].ToString();
box3.Text = dt.Rows[i]["Item Description"].ToString();
box8.Text = dt.Rows[i]["Order Quantity"].ToString();
box12.Text = dt.Rows[i]["UOM"].ToString();
box16.Text = dt.Rows[i]["Rate"].ToString();
box20.Text = dt.Rows[i]["Currency"].ToString();
box24.Text = dt.Rows[i]["Amount"].ToString();
rowIndex++;
}
}
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
addnewrow();
}
請在下方進行更改....
1)在addnew()函數更改下面的代碼行:
dtCurrentTable.Rows[i - 1]["Item Description"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box5.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box6.Text;
更改代碼如下:
dtCurrentTable.Rows[i - 1]["Item Description"] = box3.Text + "," + box4.Text + "," + box5.Text + "," + box6.Text;
同樣的邏輯適用於訂單數量,UOM,費率,貨幣和金額字段/控件的商店價值
2)在SetPreviousData()函數下面改變代碼行:
box3.Text = dt.Rows[i]["Item Description"].ToString();
更改代碼如下:
if(Convert.ToString(dt.Rows[i]["Item Description"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["Item Description"]).Trim().Split(',');
box3.Text = strDesc[0].ToString();
box4.Text = strDesc[1].ToString();
box5.Text = strDesc[2].ToString();
box6.Text = strDesc[3].ToString();
}
同樣的邏輯適用於訂單數量,UOM,費率,貨幣和金額字段/控件的商店價值
在addnewrow()函數中替換下面的代碼:
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList box2 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("DropDownList1");
box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
box2.DataTextField = "Port_Number";
box2.DataValueField = "Description";
box2.DataBind();
box2.Items.Insert(0, " ");
}
用以下代碼行替換上面的代碼......
for (int i = 0; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList box2 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("DropDownList1");
box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
box2.DataTextField = "Port_Number";
box2.DataValueField = "Description";
box2.DataBind();
box2.Items.Insert(0, " ");
DropDownList box12 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList3");
box12.DataSource = SIMBL.ShowUnitItemInfo();
box12.DataTextField = "Name";
box12.DataValueField = "Symbol";
box12.DataBind();
box12.Items.Insert(0, " ");
DropDownList box13 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList4");
box13.DataSource = SIMBL.ShowUnitItemInfo();
box13.DataTextField = "Name";
box13.DataValueField = "Symbol";
box13.DataBind();
box13.Items.Insert(0, " ");
DropDownList box14 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList5");
box14.DataSource = SIMBL.ShowUnitItemInfo();
box14.DataTextField = "Name";
box14.DataValueField = "Symbol";
box14.DataBind();
box14.Items.Insert(0, " ");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.