2014-10-28 94 views
0

我有一個從sql數據庫datagridview和它的數據。如何將一些文本設置爲datagridview單元格,當它的單元格在c#中包含0值時?

我的datagridview一些單元格填充0(零)值。
我想在datagridview中填充0值的位置設置NULL文本。

enter image description here

//I'm using this code but not working 
private void dataGridView4_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) 
    { 
     for (int i = 0; i < dataGridView4.Rows.Count; i++) 
     { 
      for (int j = 7; j < dataGridView4.Rows[i].Cells.Count; j++) 
      { 
       if (Convert.ToInt32(dataGridView4.Rows[i].Cells[j].Value) == 0) 
       { 
        dataGridView4.Rows[i].Cells[j].Value = "Null"; 
       } 

      } 
     } 

    } 

我的SQL查詢:

select l.admission_number,l.student_class ,l.student_name,hindi,maths,social,l.telugu,l.english , '' as english_1,'' as english_2,l.science, '' as ns,'' as ps from lkg_to_seventh_marks as l union all select e.admission_number ,e.student_class , e.student_name ,hindi,maths,social,e.telugu,'' as english,e.english_1 ,e.english_2,'' as science,e.ns,e.ps from eighth_to_ninth_marks as e

+0

我想你不能手動操作字段,因爲你是數據綁定。 – RadioSpace 2014-10-28 06:28:19

+0

通過什麼方式獲得「數據」,即數據集/ SQLserver,EF/MySQL,EF/SQLserver? – RadioSpace 2014-10-28 06:29:53

+0

是啊..當然我綁定的數據,但我想設置一些文本在0值..如果有任何可能性告訴我PLZ.Here我綁定數據從Microsoft SQL服務器 – 2014-10-28 06:31:25

回答

0

正如alireza說我更換任何0在我的SQL查詢與 'NULL' 值與casting.for 3個表有一些不同的列和一些相同的列

select l.admission_number,l.student_class ,l.student_name,cast(l.telugu as char(4)) as telugu, 
 
'NULL' as telugu_1,'NULL' as telugu_2,l.hindi,cast(l.english as CHAR(4)) as english,'NULL' as english_1,'NULL' as english_2, 
 
cast(l.maths as CHAR(4)) as maths,'NULL' as maths_1,'NULL' as maths_2, 
 
cast(l.science as CHAR(4)) as science,'NULL' as ns,'NULL' as ps, 
 
cast(l.social as CHAR(4)) as social,'NULL' as social_1, 
 
'NULL' as social_2,l.total 
 
from lkg_to_seventh_marks as l 
 

 
union all 
 

 
select e.admission_number ,e.student_class ,e.student_name , 
 
cast(e.telugu as char(4)) as telugu,'NULL'as telugu_1,'NULL' as telugu_2,e.hindi, 
 
'NULL' as english,cast(e.english_1 as CHAR(4)) as english_1, 
 
CAST(e.english_2 as CHAR(4)) as english_2,CAST(e.maths as CHAR(4)) as maths,'NULL' as maths_1, 
 
'NULL' as maths_2,'NULL' as science,cast(e.ns as CHAR(4)) as ns,CAST(e.ps as CHAR(4)) as ps, 
 
'NULL' as social,'NULL' as social_1,'NULL' as social_2,e.total 
 
from eighth_to_ninth_marks as e 
 

 
union all 
 

 
select t.admission_number ,t.student_class ,t.student_name,'NULL'as telugu, 
 
cast(t.telugu_1 as char(4)) as telungu_1,CAST(t.telugu_2 as CHAR(4)) as telugu_2, 
 
t.hindi,'NULL' as english,CAST(t.english_1 as CHAR(4)) as english_1, 
 
CAST(t.english_2 as CHAR(4)) as english_2,'NULL' as maths,CAST(t.maths_1 as CHAR(4)) as maths_1, 
 
CAST(t.maths_2 as CHAR(4)) as maths2,'NULL' as science,CAST(t.ns as CHAR(4)) as ns,CAST(t.ps as CHAR(4))as ps, 
 
'NULL' as social,CAST(t.social_1 as CHAR(4)) as social_1,CAST(t.social_2 as CHAR(4)) as social_2,t.total 
 
from tenthclass_marks as t

1

假設你想更換的0與字符串中的每個例如,一個做這將是這樣使用DataGridView的RowsAdded事件。通過DataGridViewRowEventArgs獲取對該行的引用,迭代該行中的每個DataGridViewItem,然後將您遇到的任何0值更改爲任何要替換的值。

這裏是事件的一個例子用法:

private void dataGridView1_NewRowNeeded(object sender, 
     DataGridViewRowEventArgs e) 
{ 
    foreach (DataGridViewCell cell in e.Row.Cells) 
    { 
     if (cell.Value == "0" || cell.Value == 0) // not sure which you're using 
     { 
      cell.Value = "your replacement value here"; 
     } 
    } 
} 

MSDN對RowsAdded這裏的一篇文章:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded%28v=vs.110%29.aspx

注意RadioSpace和阿里是正確的 - 你已經經過UI控件修改元素已經餵它,你的數據有點落後和過於複雜。

+0

你有一個代碼示例來實現這一點,而數據綁定? – RadioSpace 2014-10-28 06:38:06

+0

現在添加一個。我假設他使用WinForms,因爲DataGridView不存在於WPF中。我不認爲他是數據綁定的事實會很重要 - 我對WPF中的DataGrid使用了類似的修復。不過,他仍然應該測試一下。 – furkle 2014-10-28 06:39:06

+0

是我使用的贏形成 – 2014-10-28 06:47:50

2

您可以直接從查詢中返回'NULL'而不是0。要注意的是返回的值不會成爲這個變更後的數字,所以用這個查詢只用於填充網格:

SELECT l.admission_number,l.student_class, 
    l.student_name, 
    CASE hindi WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), hindi), 
    CASE maths WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), maths), 
    CASE social WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), social), 
    CASE l.telugu WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), l.telugu), 
    CASE l.english WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), l.english), 
    '' as english_1, '' as english_2,l.science, '' as ns,'' as ps 
FROM lkg_to_seventh_marks as l 
UNION ALL 
SELECT e.admission_number ,e.student_class , 
    e.student_name, 
    CASE hindi WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), hindi), 
    CASE maths WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), maths), 
    CASE social WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), social), 
    CASE e.telugu WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), e.telugu), 
    '' as english, 
    CASE e.english_1 WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), e.english_1), 
    CASE e.english_2 WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), e.english_2), 
    '' as science, 
    CASE e.ns WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), e.ns), 
    CASE e.ps WHEN 0 THEN 'NULL' ELSE CONVERT(varchar(15), e.ps) 
FROM eighth_to_ninth_marks as e 

我希望你的想法

+0

我要這樣說我的表執行後,我的查詢,並直接裝牛逼的datagridview – 2014-10-28 09:40:27

+0

確定沒有內容0 value..I得到0值。所以你已經隱藏了我們的一些東西:D – Alireza 2014-10-28 09:46:35

+0

我不知道我從你身上隱藏什麼。 – 2014-10-28 09:58:54

相關問題