本文介绍了网格特色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统; 使用 System.Collections; 使用 System.Configuration; 使用 System.Data; 使用 System.Linq; 使用 System.Web; 使用 System.Web.Security; 使用 System.Web.UI; 使用 System.Web.UI.HtmlControls; 使用使用System.Web.UI.WebControls; 使用 System.Web.UI.WebControls.WebParts; 使用 System.Xml.Linq; 公共 部分 类 Default16:System.Web .UI.页面 { int result1,result2,result3; 私有 无效 SetInitialRow() { DataTable dt = 新 DataTable(); DataRow dr = 空; dt.Columns.Add(新 DataColumn(" RowNumber" ,类型(字符串)))); dt.Columns.Add(新 DataColumn(" Column1" ,类型(字符串)))); dt.Columns.Add(新 DataColumn(" Column2" ,类型(字符串)))); dt.Columns.Add(新 DataColumn(" Column3" ,类型(字符串)))); dt.Columns.Add(新 DataColumn(" Column4" ,类型(字符串)))); dt.Columns.Add(新 DataColumn(" Column5" ,类型(字符串)))); dt.Columns.Add(新 DataColumn(" Column6" ,类型(字符串)))); dr = dt.NewRow(); dr [" 行数" ] = 1 ; dr [" Column1" ] = 字符串 .空的; dr [" Column2" ] = 字符串 .空的; dr [" Column3" ] = 字符串 .空的; dr [" Column4" ] = 字符串 .空的; dr [" Column5" ] = 字符串 .空的; dr [" Column6" ] = 字符串 .空的; dt.Rows.Add(dr); // 将数据表存储在ViewState中 ViewState [" CurrentTable" ] = dt; Gridview1.DataSource = dt; Gridview1.DataBind(); DropDownList ddl1 =(DropDownList)Gridview1.Rows [ 0 ].Cells [ 2 ].FindControl(" DropDownList1" ); FillDropDownList(ddl1); DropDownList ddl2 =(DropDownList)Gridview1.Rows [ 0 ].Cells [ 3 ].FindControl(" DropDownList2" ); FillDropDownList1(ddl2); } 私有 无效 AddNewRowToGrid() { int rowIndex = 0 ; 如果(ViewState [" CurrentTable" ] != 为空) { DataTable dtCurrentTable =(DataTable)ViewState [" CurrentTable" ]; DataRow drCurrentRow = 空; int icount,计数; icount = Gridview1.Rows.Count; 计数= Gridview1.Rows.Count; 如果(icount == 1 ) { icount = 0 ; } 如果(dtCurrentTable.Rows.Count > 0 跨度>) { for ( int i = icount; i >< = count; i ++) { // 提取TextBox值 如果(i == 0 ) { dtCurrentTable.Rows [ 0 ] [" RowNumber" 跨度>] = i + 1; dtCurrentTable.Rows [ 0 ] [" Column1" span>] = 字符串 .Empty; dtCurrentTable.Rows [ 0 ] [" Column2" span>] = 字符串 .Empty; dtCurrentTable.Rows [ 0 ] [" Column3" span>] = 字符串 .Empty; dtCurrentTable.Rows [ 0 ] [" Column4" span>] = 字符串 .Empty; dtCurrentTable.Rows [ 0 ] [" Column5" span>] = 字符串 .Empty; dtCurrentTable.Rows [ 0 ] [" Column6" span>] = 字符串 .Empty; // 按钮b1 =(按钮)Gridview1.Rows [rowIndex] .Cells [6] .FindControl("ButtonAdd123") ; // b1.Visible = true; } 其他 { TextBox box1 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 1 ].FindControl(" TextBox1"); TextBox box2 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 4 ].FindControl(" TextBox2"); TextBox box3 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 5 ].FindControl(" TextBox3"); TextBox box4 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 6 ].FindControl(" TextBox4"); DropDownList ddl1 =(DropDownList)Gridview1.Rows [rowIndex] .Cells [ 2 ].FindControl(" DropDownList1"); DropDownList ddl2 =(DropDownList)Gridview1.Rows [rowIndex] .Cells [ 3 ].FindControl(" DropDownList2"); // 按钮b2 =(Button)Gridview1.Rows [rowIndex] .Cells [7] .FindControl("ButtonAdd123") ; // b2.Visible = false; drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow [" RowNumber" ] = i + 1 跨度>; dtCurrentTable.Rows.Add(drCurrentRow); dtCurrentTable.Rows [i] [" Column1" ] = box1.Text; dtCurrentTable.Rows [i] [" Column2" ] = ddl1.SelectedItem.Text; dtCurrentTable.Rows [i] [" Column3" ] = ddl2.SelectedItem.Text; dtCurrentTable.Rows [i] [" Column4" ] = box2.Text; dtCurrentTable.Rows [i] [" Column5" ] = box3.Text; dtCurrentTable.Rows [i] [" Column6" ] = box4.Text; rowIndex ++; } } // dtCurrentTable.Rows.Add(drCurrentRow); ViewState [" CurrentTable" ] = dtCurrentTable; Gridview1.DataSource = dtCurrentTable; Gridview1.DataBind(); } } 其他 { Response.Write(" ViewState为空" ); } // 设置回传的先前数据 SetPreviousData(); } 私有 无效 SetPreviousData() { int rowIndex = 0 ; 如果(ViewState [" CurrentTable" ] != 为空) { DataTable dt =(DataTable)ViewState [" CurrentTable" ]; 如果(dt.Rows.Count > 0 跨度>) { for ( int i = 0 ; i < dt.Rows.Count; i ++) { TextBox box1 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 1 ].FindControl(" TextBox1"); TextBox box2 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 4 ].FindControl(" TextBox2"); TextBox box3 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 5 ].FindControl(" TextBox3"); TextBox box4 =(TextBox)Gridview1.Rows [rowIndex] .Cells [ 6 ].FindControl(" TextBox4"); DropDownList ddl1 =(DropDownList)Gridview1.Rows [rowIndex] .Cells [ 2 ].FindControl(" DropDownList1"); DropDownList ddl2 =(DropDownList)Gridview1.Rows [rowIndex] .Cells [ 3 ].FindControl(" DropDownList2"); FillDropDownList(ddl1); FillDropDownList1(ddl2); box1.Text = dt.Rows [i] [" Column1" ].ToString(); ddl1.ClearSelection(); 如果(ddl1.Items.FindByText(dt.Rows [i] [" Column2" ].ToString())!= 空) { ddl1.Items.FindByText(dt.Rows [i] [" Column2" ].ToString()) .Selected = true ; } 如果(ddl2.Items.FindByText(dt.Rows [i] [" Column3" ].ToString())!= 空) { ddl2.Items.FindByText(dt.Rows [i] [" Column3" ].ToString()) .Selected = true ; } box2.Text = dt.Rows [i] [" Column4" ].ToString(); box3.Text = dt.Rows [i] [" Column5" ].ToString(); box4.Text = dt.Rows [i] [" Column6" ].ToString(); Button b2 =(Button)Gridview1.Rows [rowIndex] .Cells [ 7 ].FindControl(" ButtonAdd123"); 如果(i == 0 ) { b2.Visible = true ; box1.ReadOnly = 假; box2.ReadOnly = false ; box3.ReadOnly = false ; box4.ReadOnly = false ; ddl1.Enabled = 真; ddl2.Enabled = true ; } 其他 { b2.Visible = false ; box1.ReadOnly = true ; box2.ReadOnly = true ; box3.ReadOnly = true ; box4.ReadOnly = true ; ddl1.Enabled = false ; ddl2.Enabled = false ; } rowIndex ++; } } } } 私有 ArrayList GetDummyData() { ArrayList arr = 新 ArrayList(); arr.Add(新 ListItem(" Item1" span>," 1" ))); 皓arr.Add(新 ListItem(" Item2" ," 2" ))); arr.Add(新 ListItem(" Item3" span>," 3" )); arr.Add(新 ListItem(" Item4" span>," 4" ))); arr.Add(新 ListItem(" Item5" span>," 5" ))); 返回 arr; } 私有 无效 FillDropDownList(DropDownList ddl) { ArrayList arr = GetDummyData(); foreach (列表项 in arr) { ddl.Items.Add(item); } } 私有 ArrayList GetDummyData1() { ArrayList arr = 新 ArrayList(); arr.Add(新 ListItem(" KGS" span>," 1" ))); arr.Add(新 ListItem(" NOS" span>," 2" ))); arr.Add(新 ListItem(" LITRE" span>," 3" ))); arr.Add(新 ListItem(" PACKS" span>," 4" ))); arr.Add(新 ListItem(" BALE" span>," 5" ))); 返回 arr; } 私有 无效 FillDropDownList1(DropDownList ddl) { ArrayList arr = GetDummyData1(); foreach (列表项 in arr) { ddl.Items.Add(item); } } 受保护的 无效 Page_Load(对象发件人,EventArgs e) { 如果(!Page.IsPostBack) { SetInitialRow(); } } // 受保护的void ButtonAdd_Click(对象发送者,EventArgs e) // { // AddNewRowToGrid(); // } // 受保护的无效Gridview1_RowCommand(对象发送者,GridViewCommandEventArgs e) // { // 字符串cmdarg; // int rowIndex = 0; // cmdarg = e.CommandArgument.ToString(); // 按钮b2 =(Button)Gridview1.Rows [rowIndex] .Cells [4] .FindControl("ButtonAdd123") ; // 如果(cmdarg =="1") // { // b2.Visible = true; // } // 其他 // { // b2.Visible = false; // } // } 受保护的 无效 ButtonAdd123_Click(对象发件人,EventArgs e) { AddNewRowToGrid(); } 受保护的 无效 Gridview1_RowDataBound(对象发件人,GridViewRowEventArgs e) { 如果(例如,Row.RowType == DataControlRowType.DataRow) { TextBox TextBox2 =(TextBox)e.Row.FindControl(" TextBox2" ); TextBox TextBox3 =(TextBox)e.Row.FindControl(" TextBox3" ); TextBox TextBox4 =(TextBox)e.Row.FindControl(" TextBox4" ); } } // 如果(例如,Row.RowType == DataControlRowType.DataRow) // { // TextBox TextBox2 =(TextBox)e.Row.FindControl("TextBox2"); // TextBox TextBox3 =(TextBox)e.Row.FindControl("TextBox3"); // TextBox TextBox4 =(TextBox)e.Row.FindControl("TextBox4"); // 字符串ID = e.Row.Cells [4] .Text; // TextBox2.Text = id; // 字符串id2 = e.Row.Cells [5] .Text; // TextBox3.Text = id2; // int mult = int.Parse(id)* int.Parse(id2); // TextBox4.Text = mult; // e.Row.Cells [e.Row.Cells.Count-1] .Text = mult.ToString() ; // } 受保护的 无效 TextBox3_TextChanged(对象发件人,EventArgs e) { 尝试 { int 值; for ( int i = 0 ; i < Gridview1.Rows.Count; i ++) { GridViewRow行= Gridview1.Rows [i]; TextBox TextBox2 =(TextBox)row.FindControl(" TextBox2" ); TextBox TextBox3 =(TextBox)row.FindControl(" TextBox3" ); TextBox TextBox4 =(TextBox)row.FindControl(" TextBox4" ); 值 = Convert.ToInt32(TextBox2.Text)* Convert.ToInt32(TextBox3.Text); TextBox4.Text = Convert.ToString(值); } } 捕获(例外) { 扔 ex; } } }
解决方案
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Default16 : System.Web.UI.Page { int result1, result2, result3; private void SetInitialRow() { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Column1", typeof(string))); dt.Columns.Add(new DataColumn("Column2", typeof(string))); dt.Columns.Add(new DataColumn("Column3", typeof(string))); dt.Columns.Add(new DataColumn("Column4", typeof(string))); dt.Columns.Add(new DataColumn("Column5", typeof(string))); dt.Columns.Add(new DataColumn("Column6", typeof(string))); dr = dt.NewRow(); dr["RowNumber"] = 1; dr["Column1"] = string.Empty; dr["Column2"] = string.Empty; dr["Column3"] = string.Empty; dr["Column4"] = string.Empty; dr["Column5"] = string.Empty; dr["Column6"] = string.Empty; dt.Rows.Add(dr); //Store the DataTable in ViewState ViewState["CurrentTable"] = dt; Gridview1.DataSource = dt; Gridview1.DataBind(); DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("DropDownList1"); FillDropDownList(ddl1); DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList2"); FillDropDownList1(ddl2); } private void AddNewRowToGrid() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; int icount, count; icount = Gridview1.Rows.Count; count = Gridview1.Rows.Count; if (icount==1) { icount = 0; } if (dtCurrentTable.Rows.Count > 0) { for (int i = icount; i <= count; i++) { //extract the TextBox values if (i==0) { dtCurrentTable.Rows[0]["RowNumber"] = i+1; dtCurrentTable.Rows[0]["Column1"] = string.Empty; dtCurrentTable.Rows[0]["Column2"] = string.Empty; dtCurrentTable.Rows[0]["Column3"] = string.Empty; dtCurrentTable.Rows[0]["Column4"] = string.Empty; dtCurrentTable.Rows[0]["Column5"] = string.Empty; dtCurrentTable.Rows[0]["Column6"] = string.Empty; //Button b1 = (Button)Gridview1.Rows[rowIndex].Cells[6].FindControl("ButtonAdd123"); //b1.Visible = true; } else { TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2"); TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3"); TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox4"); DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList1"); DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList2"); //Button b2 = (Button)Gridview1.Rows[rowIndex].Cells[7].FindControl("ButtonAdd123"); //b2.Visible = false; drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["RowNumber"] = i + 1; dtCurrentTable.Rows.Add(drCurrentRow); dtCurrentTable.Rows[i]["Column1"] = box1.Text; dtCurrentTable.Rows[i]["Column2"] = ddl1.SelectedItem.Text; dtCurrentTable.Rows[i]["Column3"] = ddl2.SelectedItem.Text; dtCurrentTable.Rows[i]["Column4"] = box2.Text; dtCurrentTable.Rows[i]["Column5"] = box3.Text; dtCurrentTable.Rows[i]["Column6"] = box4.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 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[1].FindControl("TextBox1"); TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2"); TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3"); TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox4"); DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList1"); DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList2"); FillDropDownList(ddl1); FillDropDownList1(ddl2); box1.Text = dt.Rows[i]["Column1"].ToString(); ddl1.ClearSelection(); if (ddl1.Items.FindByText(dt.Rows[i]["Column2"].ToString()) != null) { ddl1.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true; } if (ddl2.Items.FindByText(dt.Rows[i]["Column3"].ToString()) != null) { ddl2.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true; } box2.Text = dt.Rows[i]["Column4"].ToString(); box3.Text = dt.Rows[i]["Column5"].ToString(); box4.Text = dt.Rows[i]["Column6"].ToString(); Button b2 = (Button)Gridview1.Rows[rowIndex].Cells[7].FindControl("ButtonAdd123"); if (i == 0) { b2.Visible = true; box1.ReadOnly = false ; box2.ReadOnly = false; box3.ReadOnly = false; box4.ReadOnly = false; ddl1.Enabled = true ; ddl2.Enabled = true; } else { b2.Visible = false; box1.ReadOnly = true; box2.ReadOnly = true; box3.ReadOnly = true; box4.ReadOnly = true; ddl1.Enabled = false; ddl2.Enabled = false; } rowIndex++; } } } } private ArrayList GetDummyData() { ArrayList arr = new ArrayList(); arr.Add(new ListItem("Item1","1")); 皜 arr.Add(new ListItem("Item2","2")); arr.Add(new ListItem("Item3","3")); arr.Add(new ListItem("Item4","4")); arr.Add(new ListItem("Item5","5")); return arr; } private void FillDropDownList(DropDownList ddl) { ArrayList arr = GetDummyData(); foreach (ListItem item in arr) { ddl.Items.Add(item); } } private ArrayList GetDummyData1() { ArrayList arr = new ArrayList(); arr.Add(new ListItem("KGS","1")); arr.Add(new ListItem("NOS","2")); arr.Add(new ListItem("LITRE","3")); arr.Add(new ListItem("PACKS","4")); arr.Add(new ListItem("BALE","5")); return arr; } private void FillDropDownList1(DropDownList ddl) { ArrayList arr = GetDummyData1(); foreach (ListItem item in arr) { ddl.Items.Add(item); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SetInitialRow(); } } //protected void ButtonAdd_Click(object sender, EventArgs e) //{ // AddNewRowToGrid(); //} //protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e) //{ // string cmdarg; // int rowIndex = 0; // cmdarg = e.CommandArgument.ToString(); // Button b2 = (Button)Gridview1.Rows[rowIndex].Cells[4].FindControl("ButtonAdd123"); // if (cmdarg == "1") // { // b2.Visible = true; // } // else // { // b2.Visible =false ; // } //} protected void ButtonAdd123_Click(object sender, EventArgs e) { AddNewRowToGrid(); } protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2"); TextBox TextBox3 = (TextBox)e.Row.FindControl("TextBox3"); TextBox TextBox4 = (TextBox)e.Row.FindControl("TextBox4"); } } //if (e.Row.RowType == DataControlRowType.DataRow) //{ // TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2"); // TextBox TextBox3 = (TextBox)e.Row.FindControl("TextBox3"); // TextBox TextBox4 = (TextBox)e.Row.FindControl("TextBox4"); // string id = e.Row.Cells[4].Text; // TextBox2.Text = id; // string id2 = e.Row.Cells[5].Text; // TextBox3.Text = id2; // int mult = int.Parse(id) * int.Parse(id2); // TextBox4.Text = mult; // e.Row.Cells[e.Row.Cells.Count - 1].Text = mult.ToString(); //} protected void TextBox3_TextChanged(object sender, EventArgs e) { try { int value; for (int i = 0; i < Gridview1.Rows.Count; i++) { GridViewRow row = Gridview1.Rows[i]; TextBox TextBox2 = (TextBox)row.FindControl("TextBox2"); TextBox TextBox3 = (TextBox)row.FindControl("TextBox3"); TextBox TextBox4 = (TextBox)row.FindControl("TextBox4"); value = Convert.ToInt32(TextBox2.Text) * Convert.ToInt32(TextBox3.Text); TextBox4.Text = Convert.ToString(value); } } catch (Exception ex) { throw ex; } } }
解决方案
这篇关于网格特色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!