2017-11-11 11 views
0

DataGridViewにはたくさんのデータと検索テキストボックスがあります。 私の問題は、私が小計のラベルを検索データに応じて変更したいのですが、クエリで小計SUMを選択しているため、できません。テキストボックスを使用してデータグリッドビューを検索しているときにラベルに合計値を表示する方法

SqlConnection con = new SqlConnection(str); 
con.Open(); 
SqlDataAdapter sda = new SqlDataAdapter("Select Invoice_no,Date_of_Sale,Customer_Name,Contact,Item_Code,Item_Name,Quantity,Selling_Price,Discount,Paid_Amount,Remaining,Sub_Total,Total From Sales_Order Order By Invoice_no", con); 
DataTable dt = new DataTable(); 
sda.Fill(dt); 
dgv.DataSource = dt; 

DataView dv = new DataView(dt); 
dv.RowFilter = string.Format("Item_Name LIKE '{0}%' ", Search_By_ItemName.Text); 
dgv.DataSource = dv; 

すべてのものが正常に動作しますが、その私は私の小計ラベルテキストを変更したいフィルタ に従って小計テキストを変更しない:ここでは

は私の小計ラベルコードは

テキストボックスTextchangedコード

SqlConnection con = new SqlConnection(str); 
con.Open(); 
string query = "SELECT SUM(Sub_Total) from (SELECT distinct Sub_Total FROM Sales_Order) as Sales_Order"; 
    using (SqlCommand command = new SqlCommand(query, con)) 
{ 
    object result = command.ExecuteScalar(); 
    Sub_Tot.Text = Convert.ToString(result); ; 
} 
con.Close(); 
です検索フィルターによるとどのように可能ですか?

注:私が正しくあなたを理解していればそれは、タイマーを使用して完璧に取り組んだが、私はタイマー

int sum = 0; 
for (int i = 0; i < Sale_Order_Grid.Rows.Count; i++) 
{ 
    sum += Convert.ToInt32(Sale_Order_Grid.Rows[i].Cells[8].Value); 
} 
this.lbl_SubTotal.Text = sum.ToString(); 
+0

代わりにDataTableで検索してください。 – jdweng

+0

@jdwengどのコード例ですか? – Usama

+0

何かこのように:リスト searchRows = dt.AsEnumerable()。(x => x.Field ( "Customer_Name")== Search_By_ItemName.Text).ToList(); int sum = searchRows.AsEnumerable()。Sum(x => x.Field ( "Paid_Amount")); – jdweng

答えて

0

アハを使用したくない、あなたは、検索クエリは、あなたのグリッドをループしませ応じsubtotalラベルを埋めるためにしたいとその列を1つずつ合計しますか?はい?

var totlaCol= dt.Columns.Cast<DataColumn>().SingleOrDefault(col => col.ColumnName == "SubTotal"); 
if (totalCol!= null) 
{ 
    var tableRows = dt.AsEnumerable().First(); 
    var sumVal= tableRows.Field<string>(totlaCol); 
    // or 
    //sumVal = tableRows.Field<string>(dt.Columns.IndexOf(myColumn)); 
} 

の1-:

SqlConnection con = new SqlConnection(str); 
con.Open(); 
SqlDataAdapter sda = new SqlDataAdapter("Select Invoice_no,Date_of_Sale,Customer_Name,Contact,Item_Code,Item_Name,Quantity,Selling_Price,Discount,Paid_Amount,Remaining,Sub_Total,Total,SUM(Sub_Total) as SubTotal From Sales_Order Group by Invoice_no,Date_of_Sale,Customer_Name,Contact,Item_Code,Item_Name,Quantity,Selling_Price,Discount,Paid_Amount,Remaining,Sub_Total,Total Order By Invoice_no", con); 
DataTable dt = new DataTable(); 
sda.Fill(dt); 
dgv.DataSource = dt; 

DataView dv = new DataView(dt); 
dv.RowFilter = string.Format("Item_Name LIKE '{0}%' ", Search_By_ItemName.Text); 
dgv.DataSource = dv; 

は、フィールド名でごCOL値を取得する:あなたはあなたのT-SQLでの和集合体を使用してDataTable列、のようなものからそれを得ることができる場合はtrue

あなたがSumを使用した場合は、選択した列をGroup byにする必要があります。

2 - あなただけの選択和COLに別のクエリを使用して

、3- DataTable別々に使用することができます私は前にそれをテストしていないが、私は確かにこの方法は、あなたの期待する出力を与えます。

+0

このコードを最初に実装しましょう。 – Usama

+0

私の場合、このコードは機能しません。 – Usama

関連する問題