从gridview Cell中获取值 [英] Fetching values from gridview Cell
问题描述
protected void gvDetails_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(gvDetails.DataKeys [e.RowIndex] .Value .ToString());
string username = gvDetails.DataKeys [e.RowIndex] .Values [UserName]。ToString();
TextBox txtcity =(TextBox )gvDetails.Rows [e.RowIndex] .FindControl(txtcity);
TextBox txtDesignation =(TextBox)gvDetails.Rows [e.RowIndex] .FindControl(txtstate);
con.Open();
SqlCommand cmd = new SqlCommand(更新Employee_Details set City =''+ txtcity.Text +'',Designation =' '+ txtDesignation.Text +''其中UserId =+ userid,con);
cmd.ExecuteNonQuery();
con.Close();
lblresult.Text =用户名+详细信息已成功更新;
gvDetails.EditIndex = - 1;
BindEmployeeDetails();
}
i有一个gridview ID = gv_Details。当处于编辑模式时,所需的单元格更改为文本框..
我无法理解上面的 BOLD 代码..这是一个演员吗?如果是的话,为什么我们以这种方式投射从特定单元格中获取数据..?
我也想知道 RowBound事件的使用...以及何时和我们如何使用它.. ??
谢谢。
此行获取基于来自网格的短编辑行数据中的主键值的行数据
此行根据来自网格的短编辑行数据中的主键值获取行数据
从TexBox编辑的行和类型转换中查找控件
TextBox txtcity =(TextBox)gvDetails.Rows [e.RowIndex] .FindControl(txtcity);
此编辑行中的行搜索控件和TexBox中的类型转换
TextBox txtDesignation =(TextBox)gvDetails.Rows [e.RowIndex] .FindControl(txts tate);
RowBound事件将数据绑定到girdview行,这里动态添加和删除行中的控件。
SqlCommand cmd = new SqlCommand(SELECT * FROM RNDTABLE,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
十进制和= 0;
for(int i = 0; i< GridView1.Rows.Count; i ++)
{
sum + = Convert.ToDecimal(GridView1.Rows [ I] .Cells [4] .Text.ToString());
//试试这个....这是一个例子...... //
}
Response.Write(sum);
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtstate");
con.Open();
SqlCommand cmd = new SqlCommand("update Employee_Details set City=''" + txtcity.Text + "'',Designation=''" + txtDesignation.Text + "'' where UserId=" + userid, con);
cmd.ExecuteNonQuery();
con.Close();
lblresult.Text = username + " Details Updated successfully";
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
i have a gridview ID=gv_Details .and when in edit mode the required cell changes to Textbox ..
I am unable to understand above BOLD code ..is this a casting ? if yes , why do we cast in this way to fetch the data from a particul cell ..?
I also want to know use of RowBound Event.. and when and how do we use it ..??
Thank You .
This line getting row data on the basis of primary key value in short edited row data from grid
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
This line getting row data on the basis of primary key value in short edited row data from grid
string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
This line finding control from edited row and typecast in TexBox
TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
This line finding control from edited row and typecast in TexBox
TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtstate");
RowBound Event bind data to girdview rows here dynamically add and remove controls in row.
SqlCommand cmd = new SqlCommand("SELECT * FROM RNDTABLE", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); decimal sum = 0; for (int i = 0; i < GridView1.Rows.Count; i++) { sum += Convert.ToDecimal(GridView1.Rows[i].Cells[4].Text.ToString()); //try this.... this is an example ...// } Response.Write(sum);
这篇关于从gridview Cell中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!