如何增加gridview的行号 [英] How to increment row number of a gridview

查看:93
本文介绍了如何增加gridview的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个表中获取数据到gridview.My gridview有3个文本框和一个下拉控件。我想从数据库中逐个获取数据。获取每一行后我想增加一个网格的行查看并再次获取它。



这是我的代码



I am fetching data into a gridview from a table.My gridview have 3 text box and one dropdown control.I want to fetch data one by one row from database.After fetching each row i want to increment the row of a grid view and again fetch it.

Here is my code

SqlCommand sqlCommand2 = new SqlCommand("proc_bcdet", sqlConnection);
sqlCommand2.CommandType = CommandType.StoredProcedure;
sqlConnection.Open();
sqlCommand2.Parameters.Add("@btcode", SqlDbType.Int).Value = Session["btcode"];
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = sqlCommand2;
DataSet ds = new DataSet();
adp.Fill(ds);
DataTable dt = ds.Tables[0];

int rowIndex = 0;
if (dt.Rows.Count > 0)
{
   for (int i = 0; i < dt.Rows.Count; i++)
   {
      TextBox box1 = (TextBox)gvCashReceipt.Rows[i].Cells[1].FindControl("txtlgroup");
      TextBox box2 = (TextBox)gvCashReceipt.Rows[i].Cells[2].FindControl("txtamt");
      TextBox box3 = (TextBox)gvCashReceipt.Rows[i].Cells[3].FindControl("txtnarr");
      Label lb1 = (Label)gvCashReceipt.Rows[i].Cells[4].FindControl("lbllcode");
      DropDownList ddl1 = (DropDownList)gvCashReceipt.Rows[rowIndex].Cells[0].FindControl("drprledger");

      //Fill the DropDownList with Data 
      ddl1.DataSource = module.Query("Select Name from AccMast where  LGcode in (2,3,4) order by Lcode");
      ddl1.DataBind();

      if (i < dt.Rows.Count)
      {
         //Assign the value from DataTable to the TextBox 
         box1.Text = dt.Rows[i]["Lgname"].ToString();
         box2.Text = dt.Rows[i]["Amt"].ToString();
         box3.Text = dt.Rows[i]["Narr"].ToString();
         lb1.Text = dt.Rows[i]["Lcode"].ToString();
         //Set the Previous Selected Items on Each DropDownList  on Postbacks 
         ddl1.ClearSelection();
         ddl1.Items.FindByText(dt.Rows[i]["Name"].ToString()).Selected = true;
      }

      rowIndex++;
   }
}





获取第一行后我收到错误'索引超出范围'请帮助我...





添加代码块并减少索引[/ edit]



after fetching first row i got an error 'index out of range' please help me...


[edit]Code block added and indexation reduced[/edit]

推荐答案

首先从数据库中选择所有记录(不是一个一个)。然后将网格绑定到该特定数据集,它将给出精确的网格根据数据集行的行。之后使用grid.in的行数据绑定事件,该事件访问网格行控件(您的文本框,下拉等)然后使用网格行计数和数据集按照数据集值分配它们的值行计数。
at first select all records from database (Not one by one).Then bind your grid to that particular data set , it will give exact grid rows as per data set rows.After that use row data bound event of grid.in that event access grid row controls (your text boxes,Drop down etc) & then assign them values as per data set values using grid row count & data set row count.


增加查询本身的数量。

只需在gridview中显示它。
Increment the number in the query itself.
Just display it in the gridview.


这篇关于如何增加gridview的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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