gridview始终插入模式 [英] gridview always insert mode

查看:61
本文介绍了gridview始终插入模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi



我有一个带有3列Sno,名称和年龄的gridview。 Sno我从数据库中获取,在名称和年龄列中,我只是放置了一个文本框,用户可以在其中输入它们。

和i page load我正在选择第一行并更改背景颜色。

现在当他通过鼠标点击移动到下一行时我希望下一行突出显示并可插入。在输入所有行后我想立即保存它们。



i为此尝试了不同的代码但工作不正常。



hi

I have a gridview with 3 column Sno, Name and age. Sno i am fetching from Database and in name and age column I just placed a textbox where user can enter them.
and i page load i am selecting first row and changing the background color too.
now when he moves to next row by mouse click i want the next row to be highlighted and insertable. and after entering all rows i want to save them at once.

i tried different codes for this but not working properly.

protected void Page_Load(object sender, EventArgs e)
    {

        lQry = "Select SNo from sample";
        ds = clsGen.GetData(lQry);
        GridviewSample.DataSource = ds;
        GridviewSample.DataBind();
        if (!IsPostBack)
        {
            GridviewSample.SelectedIndex = 0;
            //GridviewSample.Rows[0].BackColor = System.Drawing.Color.LightGreen;
        }
    }







protected void GridviewSample_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
      GridviewSample.Rows[GridviewSample.SelectedIndex].BackColor = System.Drawing.Color.LightGray;
  }







protected override void Render(System.Web.UI.HtmlTextWriter writer)
   {
       foreach (GridViewRow row in GridviewSample.Rows)
       {
           if (row.RowType == DataControlRowType.DataRow)
           {
               row.Attributes["onmouseover"] =
                  "this.style.cursor='hand';this.style.textDecoration='underline';";
               row.Attributes["onmouseout"] =
                  "this.style.textDecoration='none';";
               // Set the last parameter to True
               // to register for event validation.
               row.Attributes["onclick"] =
                ClientScript.GetPostBackClientHyperlink(GridviewSample,
                   "Select$" + row.DataItemIndex, true);
           }
       }
       base.Render(writer);
   }







protected void GridviewSample_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState)5)
            {
                TextBox t1 = (TextBox)e.Row.Cells[1].Controls[0];
                TextBox t2 = (TextBox)e.Row.Cells[2].Controls[0];
                if (t1 != null & t2 != null)
                {
                    t1.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=''#DDDDDD''");
                    t1.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");


                    t2.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=''#DDDDDD''");
                    t2.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
                }
            }
        }
    }





这是设计文件。< br $>




This Is the design file.

<asp:GridView ID="GridviewSample" runat="server" AutoGenerateColumns="false"

        onselectedindexchanged="GridviewSample_SelectedIndexChanged"

        onselectedindexchanging="GridviewSample_SelectedIndexChanging"

        SelectedRowStyle-BackColor="LightGray" ondatabound="GridviewSample_DataBound" >
    <Columns>
     <asp:BoundField DataField="SNo" HeaderText="SNo" />
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate >
     <asp:TextBox ID="txtname" runat="server" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Age">
    <ItemTemplate >
     <asp:TextBox ID="txtage" runat="server" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
   </asp:GridView>





请帮助...



Please Help...

推荐答案

+ row.DataItemIndex, true );
}
}
base .Render(writer);
}
" + row.DataItemIndex, true); } } base.Render(writer); }







protected void GridviewSample_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState)5)
            {
                TextBox t1 = (TextBox)e.Row.Cells[1].Controls[0];
                TextBox t2 = (TextBox)e.Row.Cells[2].Controls[0];
                if (t1 != null & t2 != null)
                {
                    t1.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=''#DDDDDD''");
                    t1.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");


                    t2.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=''#DDDDDD''");
                    t2.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
                }
            }
        }
    }





这是设计文件。< br $>




This Is the design file.

<asp:GridView ID="GridviewSample" runat="server" AutoGenerateColumns="false"

        onselectedindexchanged="GridviewSample_SelectedIndexChanged"

        onselectedindexchanging="GridviewSample_SelectedIndexChanging"

        SelectedRowStyle-BackColor="LightGray" ondatabound="GridviewSample_DataBound" >
    <Columns>
     <asp:BoundField DataField="SNo" HeaderText="SNo" />
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate >
     <asp:TextBox ID="txtname" runat="server" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Age">
    <ItemTemplate >
     <asp:TextBox ID="txtage" runat="server" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
   </asp:GridView>





请帮助...



Please Help...


您可以使用和更改根据要求调整Gridview的 SelectedRowStyle 属性。



谢谢
You can use and change according to the requirement by adjusting SelectedRowStyle property of the Gridview.

Thanks


这篇关于gridview始终插入模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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