2012-08-23 2 views
0

gridview에 항목을 추가 할 때 사용자가 파일을 (gridview 항목에서) CSV 형식으로 작성하기 전에 항목을 선택하고 편집/제거 할 수 있기를 바랍니다. '편집'을 클릭하고 정보를 업데이트하면 오류가 발생합니다. 위치 0에 행이 없습니다 (또는 편집을 위해 선택한 행). 여기 내 코드가있다.gridview RowUpdating 인덱스

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     SetFocus(parttxtbox); 
     table = new DataTable(); 
     MakeDataTable(); 
    } 
    else 
     table = (DataTable)ViewState["table"]; 

    ViewState["table"] = table; 
} 

protected void MakeDataTable() 
{ 
    table.Columns.Add("Part", typeof(string)); 
    table.Columns.Add("Quantity", typeof(Int32)); 
    table.Columns.Add("Ship-To", typeof(string)); 
    table.Columns.Add("Requested Date", typeof(string)); 
    table.Columns.Add("Shipping Method", typeof(string)); 

    //Persist the table in the Session object. 
    Session["table"] = table; 

    //Bind data to the GridView control. 
    BindData(); 
} 

protected void addbtn_Click(object sender, EventArgs e) 
{ 
    part = parttxtbox.Text.ToUpper(); 
    shipto = shiptotxtbox.Text; 
    reqdate = reqdatecal.SelectedDate.ToShortDateString(); 
    shipmthd = shipddl.SelectedItem.ToString(); 
    CreateTable(); 
} 

public void CreateTable() 
{ 
    DataRow row = table.NewRow(); 
    row["Part"] = part; 
    row["Quantity"] = qty; 
    row["Ship-To"] = shipto; 
    row["Requested Date"] = reqdate; 
    row["Shipping Method"] = shipmthd; 
    table.Rows.Add(row); 

    griditems.DataSource = table.DefaultView; 
    griditems.DataBind(); 
} 

private void BindData() 
{ 
    griditems.DataSource = table; 
    griditems.DataBind(); 
} 

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    //Retrieve the table from the session object. 
    DataTable dt = (DataTable)Session["table"]; 

    //Update the values.   
    GridViewRow row = griditems.Rows[e.RowIndex]; 
    dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem.ToString(); 


    //Reset the edit index. 
    griditems.EditIndex = -1; 
    //Bind data to the GridView control. 
    BindData(); 

    ////Somewhat works, doesn't update the part field with the text entered 
    //TextBox partedit = (TextBox)griditems.Rows[e.RowIndex].FindControl("Part"); 
    //DataRow row = table.NewRow(); 
    //row["Part"] = partedit; 
    //row["Quantity"] = qty; 
    //row["Ship-To"] = shipto; 
    //row["Requested Date"] = reqdate; 
    //row["Shipping Method"] = shipmthd; 
    //table.Rows.Add(row); 
} 

"약간 작동합니다"라고 말한 부분은 업데이트를 클릭 할 때 업데이트 된 필드에 공백을 씁니다. 당신은 당신이 세션 것 테이블에 viewstate가에서 테이블을 할당하는 오류를 "There is no row at position 0."

을받을 이유

+0

ViewState가 활성화되었습니다. –

+0

업데이트 용 SQL 문이 없어야합니까? –

+0

@JimS .: Gridview 추가/업데이트 기능 샘플 http://bit.ly/NIxCPO –

답변

1

당신이 세션 there is no row in it에 데이터 테이블을 추가하는 시간은 이잖아.

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     SetFocus(parttxtbox); 
     table = new DataTable(); 
     MakeDataTable(); 
    } 
    else 
    if(Session["table"] != null) 
     table = (DataTable)ViewState["table"]; 
} 

protected void MakeDataTable() 
{ 
    table.Columns.Add("Part", typeof(string)); 
    table.Columns.Add("Quantity", typeof(Int32)); 
    table.Columns.Add("Ship-To", typeof(string)); 
    table.Columns.Add("Requested Date", typeof(string)); 
    table.Columns.Add("Shipping Method", typeof(string)); 

    //Persist the table in the Session object. 
    Session["table"] = table; //This adds table without any record in it. 

    //Bind data to the GridView control. 
    BindData(); 
} 

데이터 테이블에 레코드를 추가 한 후 세션에 데이터 테이블을 추가하십시오.

protected void addbtn_Click(object sender, EventArgs e) 
{ 
    part = parttxtbox.Text.ToUpper(); 
    shipto = shiptotxtbox.Text; 
    reqdate = reqdatecal.SelectedDate.ToShortDateString(); 
    shipmthd = shipddl.SelectedItem.ToString(); 
    CreateTable(); 
    Session["table"] = table; 
} 

업데이트 후 griditems_RowUpdating 세션 [ "table"]을 업데이트하십시오.

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    //Retrieve the table from the session object. 
    DataTable dt = (DataTable)Session["table"]; 

    //Update the values.   
    GridViewRow row = griditems.Rows[e.RowIndex]; 
    dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem.ToString(); 


    //Reset the edit index. 
    griditems.EditIndex = -1; 
    //Bind data to the GridView control. 
    BindData(); 

    Session["table"] = dt; 
} 

참고 : 당신이 행 업데이트의 데이터에 액세스하는 방법에 대한 자세한 내용을 읽어야 적합하지 않은 업데이트 기록을 얻기 위해 시도하는 방법.

+0

글쎄, 인덱스 문제는 없지만 gridview는 업데이트 된 변경 사항을 표시하지 않고 처음에는 이전 값만 표시합니다. 입력되었습니다. –

+0

griditems_RowUpdating에서 테이블을 다시 세션에 할당하고 내 대답을 업데이트하십시오. – Adil

+0

테이블을 세션에 다시 할당하려고 시도했지만 여전히 변경 사항을 업데이트하지 않습니다. –

관련 문제