0
GridView에서 다음 코드를 통해 동적으로 행을 추가 할 수 있지만 이전에 GridView에 추가 한 레코드로 응용 프로그램을 열 때마다 응용 프로그램에 캡쳐되고 표시되도록 동일한 작업을 수행해야합니다. 모든 아이디어 ??Gridview에서 동적으로 행을 추가하는 방법은 무엇입니까?
코딩 :
은 귀하의 질문에 응용 프로그램이 열릴 때 얘기 때문에, 당신이해야 할 것은 어딘가에 그리드 뷰에 데이터를 저장하는 방법이있다![enter image description here][1] protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
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)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
//Store the DataTable in ViewState
// ViewState["CurrentTable"] = dt;
Application["CurrentTable"]=dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
//if (ViewState["CurrentTable"] != null)
if (Application["CurrentTable"] != null)
{
//DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataTable dtCurrentTable1 = (DataTable)Application["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable1.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable1.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable1.NewRow();
drCurrentRow["RowNumber"] = i + 1;
/*dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;*/
dtCurrentTable1.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable1.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable1.Rows[i - 1]["Column3"] = box3.Text;
rowIndex++;
}
dtCurrentTable1.Rows.Add(drCurrentRow);
//ViewState["CurrentTable"] = dtCurrentTable;
Application["CurrentTable"] = dtCurrentTable1;
Gridview1.DataSource = dtCurrentTable1;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
//if (ViewState["CurrentTable"] != null)
if (Application["CurrentTable"] != null)
{
//DataTable dt = (DataTable)ViewState["CurrentTable"];
DataTable dt1 = (DataTable)Application["CurrentTable"];
if (dt1.Rows.Count > 0)
{
for (int i = 0; i < dt1.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
/*box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();*/
box1.Text = dt1.Rows[i]["Column1"].ToString();
box2.Text = dt1.Rows[i]["Column2"].ToString();
box3.Text = dt1.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
}
데이터베이스 또는 텍스트 파일에 저장하지 않으면 가능하지 않습니까? 여기에 이미 사용 된 Application 변수를 사용하여 수행 할 수 있습니까? 또는 다른 유사한 개념을 사용할 수 있습니까? – dnezmp03
응용 프로그램 변수가 응용 프로그램이 닫힌 후 (즉, 브라우저 창의 닫기) 더 이상 존재하지 않으므로 손실됩니다. 브라우저를 닫았다가 다시 열어도 데이터가 유지되도록하려면 외부 저장소 (예 : 텍스트 파일/데이터베이스)가이를 수행하는 유일한 방법입니다. 이것이 Grid View가 제안하는 웹 어플리케이션 (또는 사이트)이라면, 데이터베이스를 가장 쉬운 옵션으로 제안 할 것입니다. – Nunners
마지막 점수에서 사용한 값을 검색 할 수 있도록 데이터베이스의 값을 별도의 테이블에 저장하는 몇 가지 예제를 제공 할 수 있습니까? 왜냐하면 나는 충격을 받았으므로이 방법을 사용하는 방법을 모르겠습니다. 앞으로 .. – dnezmp03