问题
I have a column in GridView, in some cells there are a value and the other cells it's NULL means empty, So I need to put a default value to the empty cells without change any thing to to the cells that have data, means any empty cells I want to change the NULL value to some data let us put as "No Data". Please Help, how can I do it?
I get the data from SQL by next code
public void showVolunteers(int x)
{
SqlCommand com = new SqlCommand("SELECT [Vol_ID]as[م],[Vol_Name]as[الاسم],[Team_Info].[team_name]as[منتمي الى فريق],[Vol_Zone]as[المنطقة],[vol_street]as[الحي],case [Vol_Sex] when '0' then 'ذكر' when'1' then 'انثى' end as[الجنس],[Vol_Date_of_Birth]as[تاريخ الميلاد],[Vol_Home_Phone]as[هاتف المنزل],[Vol_Work_Phone]as[هاتف العمل],[Vol_Mobile1]as[جوال1],[Vol_Mobile2]as[جوال2],[Vol_Email]as[الايميل],[Vol_Job]as[الوظيفة],[Vol_Affiliation]as[جهةالعمل],case [vol_Education] when '0' then 'لايوجد' when '1' then 'ابتدائي' when '2' then 'اعدادي' when '3' then 'ثانوي' when '5' then 'دبلوم تقني' when '6' then 'دبلوم مهني' when '7' then 'جامعي' when '8' then 'ماجستير' when '9' then 'دكتوراه' end as [المستوى التعليمي], [vol_Education_Place]as[المؤسسة التعليمية], [vol_Education_Department]as[التخصص], case [vol_Interesting] when '0' then 'لا يوجد' when '1' then 'صــحــي' when '2' then 'إجتــــماعــي' when '3' then 'تـــعليــم' when '4' then 'تــدريـــب' end as[الإهـــتمام], [vol_Hours]as[عدد ساعات التطوع], [vol_Notes]as[مــلاحظــات] FROM [VolunteersAffairs].[dbo].[Personal_info] left join [team_info] on [Personal_Info].[team_id] =[Team_info].[team_id]", con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet dats = new DataSet();
da.SelectCommand = com;
da.Fill(dats, "Personal_Info");
dataGridViewX1.DataSource = dats.Tables["Personal_Info"];
}
回答1:
Note: Answer was provided before the code snippet was published in the question...
If you populate the table by executing a query, the transformation should be applied to the SQL that retrieves the data.
If you are populating the columns via code or via GUI, then you could use the table's column DefaultValue property as in here: DataColumn.DefaultValue Property.
Also, consider comment kindly provided by @varocarbas below.
来源:https://stackoverflow.com/questions/19610708/set-default-column-value-for-a-specific-column-in-datagridview