如何使用自动数字参数删除gridview中的数据? [英] how to delete data in gridview with automatic number parameter?

查看:47
本文介绍了如何使用自动数字参数删除gridview中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询使用自动数字参数删除gridview中的数据..

当我编辑带有自动参数编号的字段时,如何删除不自动排序的字段..

ex:1。ROBERT 2. EMILIA 3. JOSE

当我将列移到2 EMILIA,然后在gridview 1中显示.ROBERT 3. JOSE。<删除emilia然后显示在gridview 1.ROBERT 2.JOSE ..(自动顺序)

 你的答案非常有用,谢谢..

这是我的ASPX代码:

< asp:GridView ID =   GridCustomColumn runat =  服务器 AutoGenerateColumns =   false 
ShowFooter = True CellPadding = 4 ForeColor = #333333
Width = 874px高度= 103px AllowSorting = True OnRowCancelingEdit = GridCustomCol umn_RowCancelingEdit
OnRowDeleting = GridCustomColumn_RowDeleting OnRowDataBound = GridCustomColumn_RowDataBound
OnRowEditing = GridCustomColumn_RowEditing OnRowUpdating = GridCustomColumn_RowUpdating
OnSorting = GridCustomColumn_Sorting OnPageIndexChanging = GridCustomColumn_PageIndexChanging
PageSize = 20 AllowPaging = True GridLines = >
< Columns>
< asp:TemplateField HeaderText = COLUMN >
< ItemTemplate>
< asp:标签ID = lblcolumn Width = 20px runat = server ForeColor = 黑色
Text = ' <%#Eval(column)%>' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateFiel d >
< asp:TemplateField HeaderText = TEXT >
< ItemTemplate>
< asp:Label ID = lbltextcol Width = 80px runat = server Text = ' <%#Eval(text)%>' > < / asp:Label >
< / ItemTemplate >
< EditItemTemplate>
< asp:TextBox ID = txttextcol Width = 80px runat = server Text = ' <%#Eval(text)%>' > < / asp:TextBox >
< / EditItemTemplate >
< FooterTemplate>
< asp:TextBox ID = txttextcol Width = 80px runat = server Text = ' <%#Eval(text)%>' > < / asp:TextBox >
< / FooterTemplate >
< ControlStyle Width = 80px />
< HeaderStyle ForeColor = White />
< / asp:TemplateField >
< asp:TemplateField HeaderText = SIZE >
< ItemTemplate>
< asp:标签ID = lblsize Width = 80px runat = server Text = ' <%#Eval(size)%>' > < / asp:Label >
< / ItemTemplate >
< EditItemTemplate>
< asp:TextBox ID = txtsize Width = 80px runat = server Text = ' <%#Eval(size)%>' > < / asp:TextBox >
< / EditItemTemplate >
< FooterTemplate>
< asp:TextBox ID = txtsize Width = 80px runat = 服务器 > < / asp:TextBox >
< / FooterTemplate >
< ControlStyle Width = 80px />
< HeaderStyle ForeColor = White />
< / asp:TemplateField >

< asp:TemplateField>
< / asp:GridView >





这是我的ASPX.CS代码:

 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
Bindemptydt();
}
}

私有 void Bindemptydt( )
{
// 为gridview声明数据表
DataTable dt = new DataTable();
// 将列添加到数据表
dt.Columns.Add(< span class =code-string> COLUMN);
dt.Columns.Add( TEXT);
dt.Columns.Add( SIZE);

// 为数据表dt定义数据行
DataRow dr = dt.NewRow();

// 现在将数据流添加到数据表
dt .Rows.Add(DR);
// 现在将数据表绑定到gridview
GridCustomColumn.DataSource = dt;
GridCustomColumn.DataBind();

// 现在隐藏网格视图的额外行
GridCustomColumn.Rows [ 0 ]。可见= false ;
// 从数据表中删除第0行
dt.Rows [ 0 ]删除();
dt.AcceptChanges();
// 查看到viewstate的数据表
ViewState [ columnreport] = dt;
}

protected void GridCustomColumn_RowDeleting( object sender,GridViewDeleteEventArgs e)
{
((DataTable)ViewState [ columnreport])行[e.RowIndex] .Delete();
((DataTable)ViewState [ columnreport])。AcceptChanges();

if (((DataTable)ViewState [ columnreport])。Rows.Count > 0
{

GridCustomColumn.DataSource =(DataTable)ViewState [ columnreport];

GridCustomColumn.DataBind();
}
else
{
Bindemptydt();
}
}

private void addcolumn( )
{
int i;
// 将项目添加到gridview
DataTable dt = new DataTable();
// 将视图状态分配给数据表
dt =(DataTable)ViewState [ columnreport];

for (i = 1 ; i < = Convert.ToInt32(txtcolumn_count.Text); i ++)
{

DataRow dr = dt.NewRow();
// dr [report_id] = txtreport_id.Text;
dr [ COLUMN] = < span class =code-string>
;
dr [ TEXT] = ;
dr [ SIZE] = ;

// 将数据流添加到数据表
dt。 Rows.Add(DR);
// 现在将数据表绑定到gridview
GridCustomColumn.DataSource = dt ;
GridCustomColumn.DataBind();


// 将详细信息添加到viewstate
ViewState [ columnreport] = dt;
}
}
受保护 void GridCustomColumn_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType。 DataRow)
{
Label lblSerial =(Label)e.Row.FindControl( lblcolumn );
lblSerial.Text =((GridCustomColumn.PageIndex * GridCustomColumn.PageSize)+ e.Row.RowIndex + 1 )。ToString();
}
}

受保护 void Button2_Click( object sender,EventArgs e)
{
addcolumn();
}









添加了 pre 标签。

[/编辑]

解决方案

对于 lblcolumn ,您已在Markup中绑定列数据,例如...

 <   asp:label     id   =  lblcolumn    width   =  20px    runat   =  server    forecolor   = 黑色    text   =  <% #Eval(  column)%>    xmlns:asp   =  #unknown >  <   / asp:标签 >  



再次,在内GridCustomColumn_RowDataBound ,您正在更改其值。



因此,当 GridCustomColumn_RowDeleting 被触发时如果满足以下条件,则重新绑定网格。

  if (((DataTable)ViewState [  columnreport])。Rows.Count >   0 



因此,它会转到 GridCustomColumn_RowDataBound 事件并将lblcomumn的值更改为1,2,3等...



但Markup再次将值更改为 ViewState 列数据。这就是你看到不同数据的原因。


所以,我认为,如果你从Mark up中删除列绑定,那就没问题了。尝试一次。

 <   asp:label     id   =  lblcolumn    width   =  20px    runat   =  server   < span class =code-attribute> forecolor   = 黑色    xmlns:asp   = #unknown >   Text ='<% #Eval(  column)%>'> <   / asp:label  >  


I would like to inquire about the delete the data in gridview with automatic number parameter ..
when I edit a field with auto parameter number, how do I delete a field that does not automatically sort ..
ex: 1. ROBERT 2. EMILIA 3. JOSE
when I remove the column to 2 EMILIA, then display in gridview 1. ROBERT 3. JOSE.
coding that I got when I remove emilia then display in gridview 1.ROBERT 2.JOSE .. (sequence automatically)
  Your answers will be very helpful, thanks..
THIS IS MY ASPX CODE :

 <asp:GridView ID="GridCustomColumn" runat="server" AutoGenerateColumns="false"
       ShowFooter="True" CellPadding="4" ForeColor="#333333"
       Width="874px" Height="103px" AllowSorting="True" OnRowCancelingEdit="GridCustomColumn_RowCancelingEdit"
       OnRowDeleting="GridCustomColumn_RowDeleting" OnRowDataBound="GridCustomColumn_RowDataBound"
       OnRowEditing="GridCustomColumn_RowEditing" OnRowUpdating="GridCustomColumn_RowUpdating"
       OnSorting="GridCustomColumn_Sorting" OnPageIndexChanging="GridCustomColumn_PageIndexChanging"
           PageSize="20" AllowPaging="True" GridLines="None">
           <Columns>
            <asp:TemplateField HeaderText="COLUMN">
                                  <ItemTemplate>
                                         <asp:Label ID="lblcolumn" Width = "20px" runat="server" ForeColor="Black"
                                            Text='<%# Eval("column") %>' ></asp:Label>
                                     </ItemTemplate>
                                 </asp:TemplateField>
                                 <asp:TemplateField HeaderText="TEXT">
                                     <ItemTemplate>
                                        <asp:Label ID="lbltextcol" Width = "80px" runat="server" Text='<%# Eval("text")%>'></asp:Label>
                                     </ItemTemplate>
                                       <EditItemTemplate>
                               <asp:TextBox ID="txttextcol" Width = "80px" runat="server" Text='<%# Eval("text")%>'></asp:TextBox>
                                       </EditItemTemplate>
                                       <FooterTemplate>
                               <asp:TextBox ID="txttextcol" Width = "80px" runat="server" Text='<%# Eval("text")%>'></asp:TextBox>
                                       </FooterTemplate>
                                     <ControlStyle Width="80px" />
                                     <HeaderStyle ForeColor="White" />
                                 </asp:TemplateField>
                                 <asp:TemplateField HeaderText="SIZE">
                                     <ItemTemplate>
                                         <asp:Label ID="lblsize" Width = "80px" runat="server" Text='<%# Eval("size")%>' ></asp:Label>
                                     </ItemTemplate>
                                        <EditItemTemplate>
                               <asp:TextBox ID="txtsize" Width = "80px" runat="server" Text='<%# Eval("size")%>' ></asp:TextBox>
                                       </EditItemTemplate>
                                       <FooterTemplate>
                               <asp:TextBox ID="txtsize" Width = "80px" runat="server"></asp:TextBox>
                                      </FooterTemplate>
                                     <ControlStyle Width="80px" />
                                     <HeaderStyle ForeColor="White" />
                                 </asp:TemplateField>

               <asp:TemplateField>
</asp:GridView>



THIS IS MY ASPX.CS CODE :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           Bindemptydt();
        }
    }
 
    private void Bindemptydt()
    {
        //Declare a datatable for the gridview
        DataTable dt = new DataTable();
        //Add Columns to the datatable
        dt.Columns.Add("COLUMN");
        dt.Columns.Add("TEXT");
        dt.Columns.Add("SIZE");

        //Define a datarow for the datatable dt
        DataRow dr = dt.NewRow();

        //Now add the datarow to the datatable
        dt.Rows.Add(dr);
        //Now bind the datatable to gridview
        GridCustomColumn.DataSource = dt;
        GridCustomColumn.DataBind();

        //Now hide the extra row of the grid view
        GridCustomColumn.Rows[0].Visible = false;
        //Delete row 0 from the datatable
        dt.Rows[0].Delete();
        dt.AcceptChanges();
        //View the datatable to the viewstate
        ViewState["columnreport"] = dt;
    }

protected void GridCustomColumn_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      ((DataTable)ViewState["columnreport"]).Rows[e.RowIndex].Delete();
      ((DataTable)ViewState["columnreport"]).AcceptChanges();

      if (((DataTable)ViewState["columnreport"]).Rows.Count > 0)
      {

          GridCustomColumn.DataSource = (DataTable)ViewState["columnreport"];

          GridCustomColumn.DataBind();
      }
      else
      {
          Bindemptydt();
      }
  }

    private void addcolumn()
    {
        int i;
        //Add the items to the gridview
        DataTable dt = new DataTable();
        //Assign the viewstate to the datatable
        dt = (DataTable)ViewState["columnreport"];

        for (i = 1; i <= Convert.ToInt32(txtcolumn_count.Text); i++)
        {
     
            DataRow dr = dt.NewRow();
            //dr["report_id"] = txtreport_id.Text;
            dr["COLUMN"] = "";
            dr["TEXT"] = " ";
            dr["SIZE"] = " ";

            //Add the datarow to the datatable
            dt.Rows.Add(dr);
            //Now bind the datatable to the gridview
            GridCustomColumn.DataSource = dt;
            GridCustomColumn.DataBind();


            //Add the details to viewstate also
            ViewState["columnreport"] = dt;
        }
    }
    protected void GridCustomColumn_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblSerial = (Label)e.Row.FindControl("lblcolumn");
            lblSerial.Text = ((GridCustomColumn.PageIndex * GridCustomColumn.PageSize) + e.Row.RowIndex + 1).ToString();
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        addcolumn();
    }




[Edit member="Tadit"]
Added pre tags.
[/Edit]

解决方案

For lblcolumn, you have bound "column" data in Markup like...

<asp:label id="lblcolumn" width="20px" runat="server" forecolor="Black" text="<%# Eval("column") %>" xmlns:asp="#unknown"></asp:label>


Again, inside the GridCustomColumn_RowDataBound, you are changing its value.

So, when GridCustomColumn_RowDeleting is fired, it rebinds the Grid if the below is satisfied.

if (((DataTable)ViewState["columnreport"]).Rows.Count > 0)


So, it goes to GridCustomColumn_RowDataBound Event and changes the value of lblcomumn as 1, 2, 3 etc...

But Markup again changes the value to the ViewState column data. That's why you see different data.

So, I think, if you delete the column binding from the Mark up, it will be fine. Try once.

<asp:label id="lblcolumn" width="20px" runat="server" forecolor="Black" xmlns:asp="#unknown">Text='<%# Eval("column") %>'></asp:label>


这篇关于如何使用自动数字参数删除gridview中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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