我在实现用“...”代替超长字符串 之前,GridView的编辑更新功能是可以实现的,但是,加入这个以后,点编辑后,编辑行都变为了空。这是怎么回事啊?
高手指点!!!!!!!
分不多了,尽请见谅。

解决方案 »

  1.   

    后台取值时你之前应该是row.cells[i].Text
    而你前台截取了TEXT的值,所以取不到
    你可以将完整的值付给ToolTip
    然后后台这样
    row.cells[i].ToolTip;
      

  2.   

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update tb_StuResult res_single='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',res_more='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',res_pd='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',res_total='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where stu_id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }
    错误提示:'res_single' 附近有语法错误。
      

  3.   

    update tb_StuResult set  res_single='搞清楚sql语法先~~~
      

  4.   

    调试,看取到的是什么值,cell的值不对的话重新取
      

  5.   

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update tb_Questions set que_subject='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',optionA='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',optionB='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',optionC='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',optionD='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',que_answer='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }
     public void bind()
        {
            string sqlstr = "select * from tb_Questions where que_type='多选题'";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "tb_Questions");
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv;
                string gIntro;
                if (GridView1.PageIndex == 0)
                {
                    mydrv = myds.Tables["tb_Questions"].DefaultView[i];
                    gIntro = Convert.ToString(mydrv["que_subject"]);
                    GridView1.Rows[i].Cells[0].Text = SubStr(gIntro, 6);
                }
                else
                {
                    mydrv = myds.Tables["tb_Questions"].DefaultView[i + (5 * GridView1.PageIndex)];
                    gIntro = Convert.ToString(mydrv["que_subject"]);
                    GridView1.Rows[i].Cells[0].Text = SubStr(gIntro, 6);
                }
            }
            sqlcon.Close();
        }
        public string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            string sNewStr = sString.Substring(0, nLeng);
            sNewStr = sNewStr + "...";
            return sNewStr;
        }
      

  6.   


    <asp:Panel ID="Panel1" runat="server" CssClass="style3" Height="245px" 
                        ScrollBars="Vertical" style="margin-left: 0px; " Width="535px">
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                            CellPadding="4" Font-Size="10pt" ForeColor="#333333" GridLines="None" 
                            Height="228px" OnRowCancelingEdit="GridView1_RowCancelingEdit" 
                            OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" 
                            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" 
                            PageSize="5" style="margin-left: 0px" Width="500px">
                            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                            <Columns>
                                <asp:BoundField DataField="stu_id" HeaderText="学号" ReadOnly ="true" >
                                    <ItemStyle CssClass="editcolumn" Wrap="False" />
                                </asp:BoundField>
                                <asp:BoundField DataField="Name" HeaderText="姓名" ReadOnly ="true">
                                    <ItemStyle CssClass="editcolumn1" Wrap="False" />
                                </asp:BoundField>
                                <asp:BoundField DataField="res_single" HeaderText="单选成绩">
                                    <ItemStyle CssClass="editcolumn1" Wrap="False" />
                                </asp:BoundField>
                                <asp:BoundField DataField="res_more" HeaderText="多选成绩">
                                    <ItemStyle CssClass="editcolumn1" Wrap="False" />
                                </asp:BoundField>
                                <asp:BoundField DataField="res_pd" HeaderText="判断成绩">
                                    <ItemStyle CssClass="editcolumn1" />
                                </asp:BoundField>
                                <asp:BoundField DataField="res_total" HeaderText="总分">
                                    <ItemStyle CssClass="editcolumn1" />
                                </asp:BoundField>
                                <asp:CommandField HeaderText="更新" ShowEditButton="True" ShowHeader="True">
                                    <ItemStyle CssClass="editcolumn1" Wrap="False" />
                                </asp:CommandField>
                                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" ShowHeader="True">
                                    <ItemStyle CssClass="editcolumn1" Wrap="False" />
                                </asp:CommandField>
                            </Columns>
                            <RowStyle BackColor="#E3EAEB" />
                            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#7C6F57" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
                    </asp:Panel>
      

  7.   


     SqlConnection sqlcon;
        SqlCommand sqlcom;
        string strCon = @"Data Source=.\SQLEXPRESS;Initial Catalog=db_Examination;Integrated Security=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                (e.Row.Cells[7]).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
            }
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from tb_StuResult where stu_id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            bind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update tb_StuResult set res_single='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',res_more='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',res_pd='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "' where stu_id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }
        public void bind()
        {
            string sqlstr = "select * from tb_StuResult";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "tb_StuResult");
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "stu_id" };
            GridView1.DataBind();
            sqlcon.Close();
        }
        //--------------导入Excel---------------
        protected void Button1_Click(object sender, EventArgs e)
        {
            Export("application/ms-excel", "学生成绩表.xls");
        }
        private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType; this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {    }
      

  8.   


      SqlConnection sqlcon;
        SqlCommand sqlcom;
        string strCon = @"Data Source=.\SQLEXPRESS;Initial Catalog=db_Examination;Integrated Security=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    (e.Row.Cells[7]).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
            //}
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Cells[7].Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
            
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from tb_Questions where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            bind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update tb_Questions set que_subject='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',optionA='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',optionB='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',optionC='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',optionD='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',que_answer='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }
        public void bind()
        {
            string sqlstr = "select * from tb_Questions where que_type='多选题'";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "tb_Questions");
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv;
                string gIntro;
                if (GridView1.PageIndex == 0)
                {
                    mydrv = myds.Tables["tb_Questions"].DefaultView[i];
                    gIntro = Convert.ToString(mydrv["que_subject"]);
                    GridView1.Rows[i].Cells[0].Text = SubStr(gIntro, 6);
                }
                else
                {
                    mydrv = myds.Tables["tb_Questions"].DefaultView[i + (5 * GridView1.PageIndex)];
                    gIntro = Convert.ToString(mydrv["que_subject"]);
                    GridView1.Rows[i].Cells[0].Text = SubStr(gIntro, 6);
                }
            }
            sqlcon.Close();
        }
        public string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            string sNewStr = sString.Substring(0, nLeng);
            sNewStr = sNewStr + "...";
            return sNewStr;
        }
      

  9.   

     <asp:BoundField DataField="stu_id" HeaderText="学号" ReadOnly ="true" >
                                    <ItemStyle CssClass="editcolumn" Wrap="False" />
    </asp:BoundField>换成模版列把... <asp:TemplateField HeaderText="学号">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("stu_id") %>'></asp:TextBox>
                                   </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("stu_id") %>'></asp:Label>
                                     </ItemTemplate>
       </asp:TemplateField>
      

  10.   

    你UPDATE的时候肯定出错...未将对象实力化((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim()你前台都没有TEXTBOX...
      

  11.   

    我的做法是类似#2的.先全部提出来.然后使用模板列绑定.截取长度
    不过我的编辑按钮也是模板列.呵呵.自己加入按钮的button_click事件.读取tooltip的内容
      

  12.   

    在RowDataBound事件里写:具体单元格的列数和截取长度自定if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[5].Text.Length > 5)
                {
                    e.Row.Cells[5].ToolTip = e.Row.Cells[5].Text;
                    e.Row.Cells[5].Text = e.Row.Cells[5].Text.Substring(0, 5) + "...";
                }
                else
                {
                    e.Row.Cells[5].ToolTip = e.Row.Cells[5].Text;
                }
            }