|
|
|
อยากให้ข้อมูล เลือกแล้วคง อยู่ ใน Row อ่าฮะ ต้องแก้ส่วนไหนหรอครับ |
|
|
|
|
|
|
|
สวัสดีครับ พี่ ผมสงสัย ไม่รู็จะแก้ตรงส่วนไหนของ code อยากให้มันเก็บ ข้อมูลที่ เลือกไว้ ค้าง ไว้ ใน gridview แต่ มันหาย ไป
Code (C#)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Creat_Cliam_From_A_Add_labour_Quatation : System.Web.UI.Page
{
public static int k = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
BindDropDownList();
}
}
private void SetInitialRow()
{
// BindDropDownList();
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);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}//end SetInitialRow
private void addNewRowToGrid()
{
SqlConnection con = new SqlConnection("Data Source=TONNIVA-NB;Initial Catalog=DB_Modified;Integrated Security=True");
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 drop2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList2");
// TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl(drop2.SelectedItem.ToString());
// Response.Write(drop2.SelectedItem.ToString());
//DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl(drop2.SelectedItem.ToString());
// box1.Text = drop2.SelectedItem.ToString();
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
try
{
Response.Write(drop2);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
// Response.Write(drop2.Text);
cmd.CommandText = "INSERT INTO [dbo].[Labour]([LaborCode],[Work code],[LaborName],[ModelCar],[LabourHour],[Group])VALUES('" + drop2.SelectedIndex+ "','" + box2.Text + "','" + box3.Text + "','" + box4.Text + "','" + box5.Text + "','" + box6.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
}
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i+1;
dtCurrentTable.Rows[i - 1]["Column1"] = drop2.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
dtCurrentTable.Rows[i - 1]["Column6"] = box6.Text;
rowIndex++;
}//end for
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}//end if
}//end if
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}//end addNewRowToGrid
private void SetPreviousData()
{
//BindDropDownList();
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 drop2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList2");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
drop2.DataValueField= dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
box5.Text = dt.Rows[i]["Column5"].ToString();
box6.Text = dt.Rows[i]["Column6"].ToString();
rowIndex++;
drop2.DataBind();
}
//BindDropDownList();
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
addNewRowToGrid();
BindDropDownList();
}
protected void Button7_Click(object sender, EventArgs e)
{
OpenNewWindow("result_labour_search.aspx");
}//end med
public void OpenNewWindow(string url)
{
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", url));
}
private void BindDropDownList()
{
// DropDownLrowIndexaist ddlCountries = Gridview1.FooterRow.FindControl("ddlCountries") as DropDownList;
// TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
SqlConnection connection = new SqlConnection("Data Source=TONNIVA-NB;Initial Catalog=DB_Modified;Integrated Security=True");
try
{
DataTable dt = new DataTable();
string LaborCode = string.Empty;
string Workcodee = string.Empty;
string ModelCar = string.Empty;
string Equipment = string.Empty;
string newName = string.Empty;
connection.Open();
string sqlStatement = "SELECT * FROM Labour";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
// rowIndexa++;
sqlDa.Fill(dt);
DropDownList drop2 = (DropDownList)Gridview1.Rows[k].Cells[1].FindControl("DropDownList2");
if (dt.Rows.Count > 0)
{
k++;
for (int i = 0; i <= dt.Rows.Count; i++)
{
LaborCode = dt.Rows[i]["LaborCode"].ToString();
Workcodee = dt.Rows[i]["Work code"].ToString();
ModelCar = dt.Rows[i]["ModelCar"].ToString();
Equipment = dt.Rows[i]["Equipment"].ToString();
newName = LaborCode + " ---- " + Workcodee + " | " + ModelCar + " | " + Equipment;
drop2.Items.Add(new ListItem(newName, LaborCode));
}
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
connection.Close();
}
}//end drop
}//end class
}
Tag : .NET, Web (ASP.NET), C#
|
|
|
|
|
|
Date :
2015-04-08 16:21:33 |
By :
tongiggabite |
View :
963 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดูจาก Code มาถูกทางแล้วครับ แต่ถ้าคุณไป BindData มันอีกครั้ง จะต้องไปกำหนด Event ใน RowDataBound ซึ่งจะใช้การกำหนดค่า Default ให้กับ DropDownList ได้ครับ
(C#) ASP.NET GridView Control - RowDataBound
(C#) ASP.NET DropDownlist & DataBinding
(C#) ASP.NET GridView Control - DropDownList in GridView
|
|
|
|
|
Date :
2015-04-09 10:58:23 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|