1
Answer

How to make individual cell of gridview editable?

Niveditha Gavsker

Niveditha Gavsker

12y
2.3k
1
Hi
        Here I am 1st importing the csv file to gridview, but after importing i need to edit individual cell to be edited, How to make it using C# code?
I'm importing csv file to gridview and I have used the below code, but its showing an error like "Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index".
How to Solve this?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType==DataControlRowType.DataRow)
{
Label lblSrNo = (Label)e.Row.FindControl("lblSerialNo");

LinkButton _singleClickButton=(LinkButton)e.Row.Cells[0].Controls[0];

string _jsSingle=ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");

if (Page.Validators.Count > 0)
_jsSingle = _jsSingle.Insert(11, "if(Page_CLientValidate())");


for(int columnIndex=_firstEditCellIndex ;columnIndex<e.Row.Cells.Count; columnIndex++)
{
string js=_jsSingle.Insert(_jsSingle.Length-2,columnIndex.ToString());
e.Row.Cells[columnIndex].Attributes["onclick"]=js;
e.Row.Cells[columnIndex].Attributes["style"]+="cursor:pointer;cursor:hand;";
}
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;

switch (e.CommandName)
{
case ("SingleClick"):

int _rowIndex = int.Parse(e.CommandArgument.ToString());
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
_gridView.SelectedIndex = _rowIndex;
_gridView.DataBind();

Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;

Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = true;

_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear();

ScriptManager.RegisterStartupScript(this, GetType(), "setfocus", "document.getElementById('" + _editControl.ClientID + "').focus();", true);

if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
}
break;
}
}

protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ct100", columnIndex.ToString());
}
}
}
base.Render(writer);
}



Thanks&Regards
Niveditha

Answers (1)
Next Recommended Forum