本文介绍了如何将数据添加到datatable并将其绑定到datagridview C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在将数据库逐个扫描到数据表之后添加许多项目





我尝试过:



 public void CheckItem(string itemcode)
{
string constring = @数据源=。;初始目录= pos;用户ID = sa;密码= 123;
SqlCommand objCmd = new SqlCommand();
objCmd.Parameters.Clear();
using(SqlConnection objCnn = new SqlConnection(constring))
{
objCnn.Open();
using(objCmd = objCnn.CreateCommand())
{
objCmd.CommandType = CommandType.Text;
objCmd.CommandText =SELECT * FROM Item where Item_Code = @ Item_Code;
objCmd.Parameters.Add(new SqlParameter(@ Item_Code,itemcode));

SqlDataReader myreader = objCmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(myreader);
decimal sumprice = 0;
if(dt.Rows.Count< = 0)

//这里我想将新项目添加到datatable中,之后将新项目与我在$ b之前添加的项目绑定$ b 

dt = objDT.NewRow();
dt.Columns.Add(,typeof(decimal));

dataGridView1.DataSource = dt;
 


 
}
}
}
解决方案

I want to add many items after scan them one by one from database into datatable



What I have tried:

public void CheckItem( string itemcode)
        {
            string constring = @"Data Source=.;Initial Catalog=pos;User id = sa;password=123";
            SqlCommand objCmd = new SqlCommand();
            objCmd.Parameters.Clear();
            using (SqlConnection objCnn = new SqlConnection(constring))
            {
                objCnn.Open();
                using (objCmd = objCnn.CreateCommand())
                {
                    objCmd.CommandType = CommandType.Text;
                    objCmd.CommandText = "SELECT  * FROM Items where Item_Code=@Item_Code";
                    objCmd.Parameters.Add(new SqlParameter("@Item_Code", itemcode));
                  
                    SqlDataReader myreader = objCmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(myreader);
                     decimal sumprice=0;
                    if (dt.Rows.Count <= 0)
                     
// here i want to add new item into datatable , after that bind the new item with items that i added before 
 
 
                    dt = objDT.NewRow();
                    dt.Columns.Add("", typeof(decimal));
                  
                    dataGridView1.DataSource = dt;
 
                   
 
 
                }
            }
        }
解决方案


这篇关于如何将数据添加到datatable并将其绑定到datagridview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:45