gridview数据绑定的问题 [英] Problem for gridview databind

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

问题描述

在我的项目中,我使用条形码从数据库获取图书。我使用usb条形码阅读器读取条形码。我把条形码带到文本框。我给出了以下编码,用于在textchanged事件中绑定gridview。它工作正常。我第一次显示条形码阅读器,函数正在调用两次。然后第二次我显示另一个条形码数据表具有正确的值。但是gridview显示了第一次执行的数据。



我的编码如下



In my project I m using barcode for fetch book from database. I m using usb barcode reader for read the barcode. I get the barcode to textbox. I give the following coding for bind the gridview in textchanged event. It is working fine. First time I show the barcode reader the Function is calling two times. And then Second time I show the another barcode the datatable has correct value. But the gridview show the datas for first time execution.

My codings are following

private void Add_IssueDet()
  {

      try
      {
          string barcode_id = (string)Session["Pin2Value"];
          //string barcode_id = "0005427036";
          if (barcode_id != "")
          {
              DataTable dtIssueDet;
              if (Session["dtIssueDet"] == null)
                  PrepareTable();
              dtIssueDet = (DataTable)Session["dtIssueDet"];


              DataRow drIssueDet = dtIssueDet.NewRow();
              DataTable Issueempty = new DataTable();
              DataTable dtbookdetails = new DataTable();

              dtbookdetails = objDB.Get_BooksDetails_By_Barcode(barcode_id);
              //  bool isedit = (bool)ViewState["IsEdit"];
              bool isedit = false;
              if (isedit != true)
              {
                  drIssueDet["IssueDetailsId"] = dtIssueDet.Rows.Count;
                  drIssueDet["IssueId"] = objDB.GetMaxIDForIssue();
              }
              else
              {
                  drIssueDet["IssueDetailsId"] = 0;
                  if (dtIssueDet.Rows.Count == 0)
                  {
                      drIssueDet["IssueId"] = ViewState["LibIssueID"];
                  }
                  else
                  {
                      drIssueDet["IssueId"] = Convert.ToInt32(dtIssueDet.Rows[0]["IssueId"]);
                  }
              }
              if (dtbookdetails.Rows.Count > 0)
              {
                  drIssueDet["BookCategoryName"] = dtbookdetails.Rows[0]["CategoryName"].ToString();
                  drIssueDet["CategoryId"] = dtbookdetails.Rows[0]["CategoryID"].ToString();
                  drIssueDet["TitleName"] = dtbookdetails.Rows[0]["Title"].ToString();
                  drIssueDet["StockID"] = dtbookdetails.Rows[0]["Book_ID"].ToString();
                  //SessionConfig.Current.LibQuantity = objDA.GetQty(SessionConfig.Current.LibStockID);
                  drIssueDet["ReservedCode"] = "-";
                  drIssueDet["ReservedID"] = 0;
                  drIssueDet["ReservedDate"] = "-";
                  drIssueDet["Quantity"] = 1;
                  ViewState["LibQuantity"] = Convert.ToInt32(dtbookdetails.Rows[0]["stock"]);
                  drIssueDet["Description"] = "";
                  dtIssueDet.Rows.Add(drIssueDet);
                  Session["dtIssueDet"] = dtIssueDet;
                  //Session["Pin2Value"] = null;
                  grvIssueDet.DataSource = dtIssueDet;
                  grvIssueDet.DataBind();



                  GridView1.DataSource = dtIssueDet;
                  GridView1.DataBind();

              }
          }
      }

      catch (Exception ex)
      {
          objLog.ErrorLog("LibIssueDetails", "Add_IssueDet", ex.Message.ToString(), KansasErrorLog.ErrorLogMethods.TextFile, "KansasDEMS");
      }
  }
  protected void txt_barcode_id_TextChanged(object sender, EventArgs e)
{
      Session["Pin2Value"] = txt_barcode_id.Text;

      Add_IssueDet();
      //txt_barcode_id.Text = "";
  }





我不知道是什么问题。当我第一次显示条形码时,代码执行两次,它将相同的数据绑定两次并显示给gridview。第二次显示条形码时,它获取数据并绑定数据表,但gridview仅显示第一次执行数据。请帮助我.....这个问题



然后我检查,文本框autopostback是假的,它执行一次。数据表已正确获取值。但gridview只显示数据表的第一行,实际上该数据表有三行。



I didn't know what is the problem. When I show the barcode first time, the code is execute two times, it bind the same datas two times and show to the gridview. Second time I show the barcode, It fetch the datas and bind the datatable, but the gridview displays only first time execution datas. Pls help me.....for this issue

And then I check, The textbox autopostback is false, It execute one time. the datatable has get the value correctly. But the gridview is show only first row of the datatable, actually that datatable has three rows.

推荐答案

为什么从会话中获取texbox的值?

直接从文本框中获取。

无需在会话中存储文本框的值。
Why you fetch your texbox's value from session?
Fetch directly from the textbox.
No need to store textbox's value in session.


我发现了问题,为什么会这样?因为我在文本框中使用日历Extendar和gridview。如果我删除日历扩展程序,它会正确绑定和查看。
I found the problem, why it is? Because I m using calendar Extendar in Textbox with in gridview. If I remove the calendar extender, it Binds and view correctly.


这篇关于gridview数据绑定的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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