移动GridView Rows Up&在GridView控件中向下 [英] Moving GridView Rows Up & Down in a GridView Control

查看:92
本文介绍了移动GridView Rows Up&在GridView控件中向下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridview中绑定一些数据,我想在gridview中向上/向下移动(rowss Data),任何人都可以帮我怎么做。 thankyou。

解决方案

请参阅此处,

如何使用asp.net在gridview中上下移动行? [ ^ ]


万一你的网格行中有向上和向下按钮



 <   asp:GridView     ID   =  gv    runat   =  server    AutoGenerateColumn s   = 错误    OnRowCommand   =  gv_RowCommand >  
< < span class =code-keyword>>
< asp:TemplateField > ............ < / asp:TemplateField >
< asp:TemplateField > ............ < / asp:TemplateField &g t;
< asp:TemplateField > ............ < / asp: TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:Button ID = btnDown runat = s erver

CausesValidation = false

CommandName = 向下 文本 = / >
< / ItemTemplate >
< / asp:TemplateField >
< / Columns >
< / asp:GridView >


protected void gvQuestion_RowCommand(Object sender,GridViewCommandEventArgs e)
{
try
{
//处理Up命令
if(e.Com mandName ==Up)
{
moveUp()
}

//处理Down命令
if(e.CommandName ==向下)
{
moveDown()
}
}



private void moveUp()
{
if(dataGridView1.RowCount> 0)
{
if(dataGridView1.SelectedRows.Count> 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells [0] .OwningRow.Index;

if(index == 0)
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;

//删除上一行并将其添加到所选行的后面。
DataGridViewRow prevRow = rows [index - 1];
rows.Remove(prevRow);
prevRow.Frozen = false;
rows.Insert(index,prevRow);
dataGridView1.ClearSelection();
dataGridView1.Rows [index - 1] .Selected = true;
}
}
}

private void moveDown()
{
if(dataGridView1.RowCount> 0)
{
if(dataGridView1.SelectedRows.Count> 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells [0] .OwningRow.Index;

if(index ==(rowCount - 2))//包括标题行
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;

//删除下一行并将其添加到所选行的前面。
DataGridViewRow nextRow = rows [index + 1];
rows.Remove(nextRow);
nextRow.Frozen = false;
rows.Insert(index,nextRow);
dataGridView1.ClearSelection();
dataGridView1.Rows [index + 1] .Selected = true;
}
}
}


 <   blockquote     class   =  FQ >  < span class =code-keyword><   div     class   =  FQA > 引用:<   / div  >  <   html  >  
< head >

< script type = text / javascript language = javascript src = jquery.js > < / script >

< script type = text / javascript language = javascript >

I am bind some data in gridview ,i want to move(rowss Data) up/down in gridview ,can anyone help me how to do. thankyou.

解决方案

Refer here,
how to move rows up and down in gridview using asp.net?[^]


in case if you have up and down buttons in your grid rows

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" OnRowCommand="gv_RowCommand">
     <Columns>
         <asp:TemplateField>............</asp:TemplateField>
     <asp:TemplateField>............</asp:TemplateField>
     <asp:TemplateField>............</asp:TemplateField>
         <asp:TemplateField>
             <ItemTemplate>
                 <asp:Button ID="btnDown" runat="server"

                     CausesValidation="false"

                     CommandName="Down" Text="↓" />
             </ItemTemplate>
         </asp:TemplateField>
     </Columns>
</asp:GridView>


protected void gvQuestion_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    try
    {
       // Handle the Up command
       if (e.CommandName == "Up")
       {
           moveUp()
       }

       // Handle the Down command
       if (e.CommandName == "Down")
       {
           moveDown()
       }
}



private void moveUp()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;

                if (index == 0)
                {
                    return;
                }
                DataGridViewRowCollection rows = dataGridView1.Rows;

                // remove the previous row and add it behind the selected row.
                DataGridViewRow prevRow = rows[index - 1];
                rows.Remove(prevRow);
                prevRow.Frozen = false;
                rows.Insert(index, prevRow);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[index - 1].Selected = true;
            }
        }
    }

    private void moveDown()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;

                if (index == (rowCount - 2)) // include the header row
                {
                    return;
                }
                DataGridViewRowCollection rows = dataGridView1.Rows;

                // remove the next row and add it in front of the selected row.
                DataGridViewRow nextRow = rows[index + 1];
                rows.Remove(nextRow);
                nextRow.Frozen = false;
                rows.Insert(index, nextRow);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[index + 1].Selected = true;
            }
        }
    }


<blockquote class="FQ"><div class="FQA">Quote:</div><html>
<head>

    <script type="text/javascript" language="javascript" src="jquery.js"></script>

    <script type="text/javascript" language="javascript">


这篇关于移动GridView Rows Up&amp;在GridView控件中向下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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