如果数据网格包含现有数据,如何在datagrid中添加新行 [英] How can i add a new row in datagrid if the data grid contains existing data

查看:84
本文介绍了如果数据网格包含现有数据,如何在datagrid中添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我编写了一个代码,用于将文本框中的值输入到数据网格中,然后将数据网格的内容保存到数据库中
但是,如果我想从数据库中检索值,如果我想将一些新记录添加到gridview抛出文本框中,则不会happinig
如果我单击addbutton,它将在gridview的第一个位置(行索引)插入行.
我希望将其插入到网格视图的最后一个位置(如果存在3条记录,则应将其插入到网格视图的4个位置)
请帮我...

谢谢

Hi,
I have make a code for entering the values from textbox to data grid and then saving the content of data grid to database
but when i retrive the values from database if i want to add some new record into the gridview throw text box it does not happinig
if i click on addbutton , it insert the row at first position(rowindex) of gridview.
i want it to be insert at the last position of grid view (if there are 3 records present then it should be inserted on 4 position of gridview)
pls help me ...

Thanks

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            enabletrue();
            //Mode Check weather to add or edit the record.
            lblmode.Text = Request.QueryString["mode"];
            setdefaultgrid();
            if (lblmode.Text == "add")
            {
                this.txtcustomerno.Focus();
                this.btnupdate.Enabled = false;
            }
            else// if (lblmode.Text == "edit")
            {
                setvalue();
                setgridvalue();
                this.txtcustomerno.Enabled = false;
                this.txtcustomername.Focus();
                this.btnsave.Enabled = false;
               }
        }
     } 

 private void AddNewRow()
    {
        setgridvalue();
        int rowIndex = GVCustomerRate.Rows.Count;

        if (ViewState["Table"] != null)
        {
            DataTable dtTable = (DataTable)ViewState["Table"];
            DataRow drCurrentRow = null;
            //GVCustomerRate.DataSource = dtTable;
            //GVCustomerRate.DataBind();
            int tablerow = dtTable.Rows.Count;
            if (GVCustomerRate.Rows.Count >= 0)
            {
                for (int i = 0 ; i <= tablerow; i++)
                {
                    drCurrentRow = dtTable.NewRow();
                    drCurrentRow["Service No"] = txtserviceno.Text;
                    drCurrentRow["No of Axel"] = txtnoofaxel.Text;
                    drCurrentRow["Rate"] = txtrate.Text;
                    rowIndex++;
                }
                //add new row to DataTable
                dtTable.Rows.Add(drCurrentRow);
                //Store the current data to ViewState
                ViewState["Table"] = dtTable;
                //Rebind the Grid with the current data
                GVCustomerRate.DataSource = dtTable;
                GVCustomerRate.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }
 protected void setdefaultgrid()
   {
       DataTable dt = new DataTable();
       DataRow dr = null;
       dt.Columns.Add(new DataColumn("Service No", typeof(string)));
       dt.Columns.Add(new DataColumn("No of Axel", typeof(string)));
       dt.Columns.Add(new DataColumn("Rate", typeof(string)));
       dr = dt.NewRow();
       dt.Rows.Add(dr);
       ViewState["Table"] = dt;
       GVCustomerRate.DataSource = dt;
       GVCustomerRate.DataBind();
       dr.Delete();
   }

  protected void setgridvalue()
    {
        str = Request.QueryString["TrnNo"];
        cn.ConnectionString = constring;
        cn.Open();
        cmd.Connection = cn;
        cmd.CommandText = "Select ServiceNo,NoOfAxel,Rate from rate_master Where CustomerTrnId = ''" + Convert.ToInt32(str) + "''";
        da = new MySqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(ds);
        GVCustomerRate.DataSource = ds.Tables[0];
        GVCustomerRate.DataBind();
        cn.Close();
    }
protected void saveratedetails()
   {
       cn.ConnectionString = constring;     
       foreach (GridViewRow gvr in GVCustomerRate.Rows)
       {
           cn.Open();
           cmd.Connection = cn;
           cmd = new MySqlCommand("Insert_Rate_Master", cn);
           cmd.CommandType = CommandType.StoredProcedure;
          
           MySqlParameter par19 = new MySqlParameter();
           par19 = cmd.Parameters.Add("ServiceNo", MySqlDbType.VarChar);
           par19.Direction = ParameterDirection.Input;
           par19.Value = gvr.Cells[1].Text; 

           MySqlParameter par13 = new MySqlParameter();
           par13 = cmd.Parameters.Add("NoOfAxel", MySqlDbType.Int32);
           par13.Direction = ParameterDirection.Input;
           par13.Value = Convert.ToInt32(gvr.Cells[2].Text );

           MySqlParameter par14 = new MySqlParameter();
           par14 = cmd.Parameters.Add("Rate", MySqlDbType.Double);
           par14.Direction = ParameterDirection.Input;
           par14.Value = Convert.ToDouble(gvr.Cells[3].Text);

           MySqlParameter par15 = new MySqlParameter();
           par15 = cmd.Parameters.Add("UpFlag", MySqlDbType.VarChar);
           par15.Direction = ParameterDirection.Input;
           par15.Value = "SAVE";

           MySqlParameter par16 = new MySqlParameter();
           par16 = cmd.Parameters.Add("Biid", MySqlDbType.Int32);
           par16.Direction = ParameterDirection.Input;
           par16.Value = 1;

           MySqlParameter par17 = new MySqlParameter();
           par17 = cmd.Parameters.Add("LogInTime", MySqlDbType.DateTime);
           par17.Direction = ParameterDirection.Input;
           par17.Value = System.DateTime.Now;

           MySqlParameter par18 = new MySqlParameter();
           par18 = cmd.Parameters.Add("CustomerTrnId", MySqlDbType.Int32);
           par18.Direction = ParameterDirection.Input;
           par18.Value = trnno;

           int x = cmd.ExecuteNonQuery();
           cmd.Connection.Close();
           cn.Close();
       }
   
   }

推荐答案

检查本教程,有一个向网格视图动态添加新行的示例.希望对您有所帮助

http://csharpings.blogspot.com/2010/02/dinamically- add-rows-in-gridview-from.html
Check this tutorial, there is an example of adding new row dynamically to grid view..I hope it''ll help you

http://csharpings.blogspot.com/2010/02/dinamically-add-rows-in-gridview-from.html


您看过这篇文章吗?
可编辑的嵌套数据网格 [
Had you looked at this article?
Editable Nested DataGrid[^]

If you download the source code and have a look, you will get everything you need.


亲爱的sandeep

试试这个链接,我相信它将帮助您动态地在网格视图中添加新行.
http://codeasp.net/articles/asp.net/67/adding-dynamic-rows-in-gridview-with-textboxes [
dear sandeep

try this link it i am sure it will help you to add new row in grid view dynamically..

http://codeasp.net/articles/asp.net/67/adding-dynamic-rows-in-gridview-with-textboxes[^]

if you get your answer then please rating me

thanks
DX-ARMY

[Corrected link formatting]


这篇关于如果数据网格包含现有数据,如何在datagrid中添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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