rowindex总是从零开始......为什么?

时间:2014-03-25 03:01:26

标签: c# asp.net

这是我的代码..而编辑和删除行索引始终采取零on-wards ...删除命令根本没有工作....如果我尝试编辑任何东西只有第2行以后它的工作...删除命令一点也不工作..我认为这是因为行索引..请任何人帮助我提前感谢....

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class Manager_Payments : System.Web.UI.Page
{
    //SqlConnection con =new SqlConnection("Data Source=sqlexpress;Initial Catalog=isoqrmssys;User ID=sa;password=123456;Integrated Security=True");

    Business BL = new Business();

     //protected Int64 stf_ID, vmember;
    //protected DateTime SRDT;
   private System.Drawing.Color a;
    string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        loadgridview();
    }

    private void loadgridview()
    {

        SqlConnection con = new SqlConnection(myStr);
        SqlCommand cmd = new SqlCommand("select * from CustomerProfMain", con);
        //string sql = "SELECT * FROM CustomerProfMain";
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        DataSet ds = new DataSet();
        sda.Fill(ds);
        //return ds.Tables[0];
        Grd_View.DataSource = ds.Tables[0];
        Grd_View.DataBind();
        con.Close();
    }


    protected void Grd_View_RowCommand(Object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
            {

                int index = Grd_View.SelectedIndex;
                if (e.CommandName == "Edit")
                {
                    //string RowIndex = int.Parse(e.CommandArgument.ToString());
                   // Session["rowid"] = RowIndex;
                    DataTable dt = new DataTable();
                    SqlConnection con = new SqlConnection(myStr);
                    SqlCommand cmd = new SqlCommand("Select * from CustomerProfMain where CustomerCode='" + e.CommandArgument.ToString() + "'", con);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    con.Open();
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    dt=ds.Tables[0];
                    TextBox1.Text = dt.Rows[0]["CustomerName"].ToString();
                    TextBox2.Text=dt.Rows[0]["Address"].ToString();
                    TextBox3.Text=dt.Rows[0]["TellNo"].ToString();
                    TextBox4.Text=dt.Rows[0]["FaxNo"].ToString();
                    TextBox5.Text=dt.Rows[0]["Email"].ToString();
                    Button1.Text = "Update";

                }
                if (e.CommandName == "Delete")
                {
                    int RowIndex = int.Parse(e.CommandArgument.ToString());
                   Session["rowid"] = RowIndex;
                   // DataTable dt = new DataTable();
                    SqlConnection con = new SqlConnection(myStr);
                    SqlCommand cmd = new SqlCommand("Delete from CustomerProfMain where CustomerCode='" + RowIndex + "' ", con);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();


                }
              }
    protected void Grd_View_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void Grd_View_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void Grd_View_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Button1.Text == "Add")
        {
            string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
            SqlConnection con = new SqlConnection(myStr);
            con.Open();
            string sql = string.Empty;
            sql = "insert into CustomerProfMain(CustomerName,Address,TellNo,FaxNo,Email) values('" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + TextBox4.Text.Trim() + "','" + TextBox5.Text.Trim() + "') ";
            SqlCommand cmd = new SqlCommand(sql, con);

            cmd.ExecuteNonQuery();
            con.Close();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            Button1.Text = "Add";
            loadgridview();
        }
        if (Button1.Text == "Update")
        {
            string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
            SqlConnection con = new SqlConnection(myStr);
            con.Open();
            string sql = string.Empty;
            sql = "update CustomerProfMain set CustomerName='" + TextBox1.Text.Trim() + "',Address='" + TextBox2.Text.Trim() + "',TellNo='" + TextBox3.Text.Trim() + "',FaxNo='" + TextBox4.Text.Trim() + "',Email='" + TextBox5.Text.Trim() + "' where CustomerCode='" + Session["rowid"] + "'";
            SqlCommand cmd = new SqlCommand(sql, con);

            cmd.ExecuteNonQuery();
            con.Close();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            Button1.Text = "Add";
            loadgridview();
        }

    }



}



"<asp:GridView ID="Grd_View" ShowFooter="True" runat="server" OnRowEditing="Grd_View_RowEditing" AutoGenerateColumns="False"
                     DataKeyNames="CustomerCode" cellpadding="4" OnRowCommand="Grd_View_RowCommand"                  GridLines="None" 
                    AllowPaging="True" AllowSorting="True" CssClass="style2" ForeColor="#333333" Width="569px" OnRowDataBound="Grd_View_RowDataBound" OnRowDeleting="Grd_View_RowDeleting">
                    <FooterStyle BackColor="#555555" ForeColor="White" Font-Bold="True" />
                    <Columns>
                        <asp:BoundField DataField="CustomerCode" HeaderText="CustomerCode" InsertVisible="False"
                            ReadOnly="True" SortExpression="CustomerCode" />

                        <asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" />
                        <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                        <asp:BoundField DataField="TellNo" HeaderText="TellNo" SortExpression="TellNo" />
                        <asp:BoundField DataField="FaxNo" HeaderText="FaxNo" SortExpression="FaxNo" />
                        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                        <asp:CommandField ShowEditButton="true" SelectText="Edit" />
                        <asp:CommandField ShowDeleteButton="true" SelectText="Delete" />

                    </Columns>
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <PagerStyle BackColor="#777777" ForeColor="White" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#555555" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                </asp:GridView>

&#34;

3 个答案:

答案 0 :(得分:1)

使用此代码替换网格视图

 <asp:GridView ID="Grd_View" ShowFooter="True" runat="server" OnRowEditing="Grd_View_RowEditing" AutoGenerateColumns="False"
                DataKeyNames="CustomerCode" CellPadding="4" OnRowCommand="Grd_View_RowCommand" GridLines="None"
                AllowPaging="True" AllowSorting="True" CssClass="style2" ForeColor="#333333" Width="569px" OnRowDataBound="Grd_View_RowDataBound" OnRowDeleting="Grd_View_RowDeleting">
                <FooterStyle BackColor="#555555" ForeColor="White" Font-Bold="True" />
                <Columns>
                    <asp:BoundField DataField="CustomerCode" HeaderText="CustomerCode" InsertVisible="False"
                        ReadOnly="True" SortExpression="CustomerCode" />

                    <asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" />
                    <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                    <asp:BoundField DataField="TellNo" HeaderText="TellNo" SortExpression="TellNo" />

                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnEdit" runat="server" CommandArgument='<%#Eval("CustomerCode")%>' CommandName="Edit" Text="Edit">
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%#Eval("CustomerCode")%>' CommandName="Delete" Text="Delete">
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#777777" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#555555" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

            </asp:GridView>

您没有设置CommandArgument,我们将其设置为CommandArgument='<%#Eval("CustomerCode")%>'到您的编辑按钮并删除按钮

答案 1 :(得分:0)

尝试执行以下操作...在页面上加载绑定网格,如果它不是回发...

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        loadgridview();
}

然后在命令事件处理程序的末尾重新绑定网格,为了清楚起见,我将删除一些数据访问逻辑......

protected void Grd_View_RowCommand(Object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {

            int index = Grd_View.SelectedIndex;
            if (e.CommandName == "Edit")
            {
               //...
               loadgridview();
            }
            if (e.CommandName == "Delete")
            {
               //...
               loadgridview();
            }
          }

答案 2 :(得分:0)

尝试此操作并将GridView的AutoPostback属性设置为true

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        loadgridview();
    }
}