在编辑时从gridview获取Cell值 [英] Getting the Cell value from gridview on Edit

查看:146
本文介绍了在编辑时从gridview获取Cell值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家,这是我想要完成的。我在gridview的itemtemplate中有一个lable显示部门名称。当我单击编辑链接按钮(在Itemtemplate内)时,将显示部门的edittemplate,它是一个下拉列表。但是在我填充此下拉列表后,我无法选择最初在该字段中的值。我在rowcommand事件处理程序中编写了以下代码...

  else   if (e.CommandName.ToLower()。Equals(  edit))
{
GridViewRow grdRow =(GridViewRow)(((LinkBut​​ton)e.CommandSource).NamingContainer);
// int nIndex = grdRow.RowIndex;
strDeptSelectedValue = grdRow.Cells [ 3 ]。文字;
strPositionSelectedValue = grdRow.Cells [ 4 ]。文字;
}





i希望得到行的Cell内容并将其存储在静态字符串中......我想要能够在RowEditing Eventhandler中使用此字符串,如下所示...

 DropDownList drpDlistEditDept =(DropDownList)grdQuestionGroup.Rows [e.NewEditIndex] .FindControl(< span class =code-string>  drpdlistEditDept); 
DropDownList drpDlistEditPosition =(DropDownList)grdQuestionGroup.Rows [e.NewEditIndex] .FindControl( drpDlistEditPosition);

ds = DepartmentDA.SelectAll();
drpDlistEditDept.DataSource = ds.Tables [ 0 ];
drpDlistEditDept.DataTextField = 名称;
drpDlistEditDept.DataValueField = 代码;
drpDlistEditDept.SelectedValue = // 这里的静态字符串。
drpDlistEditDept.DataBind ();





但静态字符串总是一个字符串。为什么Cell [3] .Text和Cell [4] .Text没有给出任何值?我已经调试并检查了... grdRow正在获取它应该的值...

解决方案

在下拉列表数据绑定后设置下拉列表的选定值



 drpDlistEditDept.DataBind(); 
drpDlistEditDept.SelectedValue = // 这里的静态字符串。





- 在这里更新回答



立即尝试这些变化,





首先,在这样的RowCommand事件中获取你的Gridview



< pre lang =c#> protected void gvProduct_RowCommand( Object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == AddNewRow
{
GridView gvProduct =(GridView)sender;

}
}





如果你仍然没有得到细胞值,然后尝试跟随更改。你一定会得到你所需的价值。



使用标签找到控件以获取GridView RowCommand事件中的值。



标签lblValue1 =(标签)gvData.Rows [rowIndex] .FindControl(  lblValue1\" ); 
Label lblValue2 =(Label)gvData.Rows [rowIndex] .FindControl( lblValue2 );
strDeptSelectedValue = lblValue1.Text;
strPositionSelectedValue = lblValue2.Text;


Hello experts, here is what i want to accomplish. I have a lable displaying Department name in the itemtemplate of gridview. When i click Edit linkbutton(inside the Itemtemplate) the edittemplate for the department which is a dropdownlist will be shown. But after i populate this dropdownlist i can''t get it to select the value that was initially in that field. I have written the following code in the rowcommand event handler...

else if (e.CommandName.ToLower().Equals("edit"))
            {
                GridViewRow grdRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                //int nIndex = grdRow.RowIndex;
                strDeptSelectedValue = grdRow.Cells[3].Text;
                strPositionSelectedValue = grdRow.Cells[4].Text;
            }



i want to get the Cell content of the row and store it in a static string...and i want to be able to utilize this string in the RowEditing Eventhandler as follows...

DropDownList drpDlistEditDept = (DropDownList)grdQuestionGroup.Rows[e.NewEditIndex].FindControl("drpdlistEditDept");
            DropDownList drpDlistEditPosition = (DropDownList)grdQuestionGroup.Rows[e.NewEditIndex].FindControl("drpDlistEditPosition");

            ds = DepartmentDA.SelectAll();
            drpDlistEditDept.DataSource = ds.Tables[0];
            drpDlistEditDept.DataTextField = "Name";
            drpDlistEditDept.DataValueField = "Code";
            drpDlistEditDept.SelectedValue=//The static string here.
            drpDlistEditDept.DataBind();



however the static string always comes out to be a "" string. Why is the Cell[3].Text and Cell[4].Text not giving any value?? I have debugged and checked...the grdRow is getting the value as it should...

解决方案

Set selected value of dropdown after your dropdownlist databind

drpDlistEditDept.DataBind();
drpDlistEditDept.SelectedValue=//The static string here.



-- UPDATED ANSWER HERE

Try these changes now,


Firstly, get your Gridview in RowCommand event like this

protected void gvProduct_RowCommand(Object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "AddNewRow")
       {
           GridView gvProduct = (GridView)sender;

       }
   }



If you still don''t get cell values, then try following changes too. You will definitely get your required values.

Use Labels by find Control for getting values in GridView RowCommand Event.

Label lblValue1 = (Label)gvData.Rows[rowIndex].FindControl("lblValue1");
Label lblValue2 = (Label)gvData.Rows[rowIndex].FindControl("lblValue2");
strDeptSelectedValue = lblValue1.Text;
strPositionSelectedValue = lblValue2.Text;


这篇关于在编辑时从gridview获取Cell值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆