为dataGridView中的特定列设置默认列值 [英] set default column value for a specific column in dataGridView

查看:158
本文介绍了为dataGridView中的特定列设置默认列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GridView中有一列,在某些单元格中有一个值,而其他单元格为空则表示为空,因此,我需要将一个默认值放入空单元格,而不改变任何具有数据的单元格,表示任何空单元格,我想将NULL值更改为某些数据,让我们将其设置为无数据。
请帮助,我该怎么办?



我从下面的代码中获取SQL的数据

  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'ذكر'作为[هاتفالعمل],[Vol_Work_Phone] as [هاتفالعمل],[Vol_Mobile1] as [جوال1],[Vol_Home_Phone] as [هاتفالمنزل],[Vol_Work_Phone] as [هاتفالعمل]作为[جاريخالميلاد]作为[الجنس],[Vol_Date_of_Birth] [جوال2],[Vol_Email] as [الايميل],[Vol_Job] as [الوظيفة],[Vol_Affiliation] as [جهةالعمل],case [vol_Education] when'0'then'لايوجد'when'1'then'ابتدائي '当'2'然后'اعدادي'当'3'然后'ثانوي'当'5'然后'دبلومتقني'当'6'然后'دبلو当'7'然后'جامعي'当'8'然后'ماجستير'当'9'然后'دكتوراه'结束为[المستوىالتعليمي],[vol_Education_Place] as [المؤسسةالتعليمية],[vol_Education_Department] as [التخصص]当'0'然后'لايوجد'当'1'然后'صحي'当'2'然后'إجتماعي'当'3'然后'تعليم'当'4'然后'تدريب'结束为[الإهتمام ] [[...]] [[]] [[]] [[]] [[]] [[]] [[]] [[]] [[] 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];
}


解决方案

注意:代码片段已发布在问题...



如果通过执行查询来填充表,则转换应该应用于检索数据的SQL。 / p>

如果您通过代码或GUI填充列,则可以使用表的列DefaultValue属性,如下所示: DataColumn.DefaultValue属性



另外,请考虑下面的@varocarbas提供的评论。


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"];
    }

解决方案

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.

这篇关于为dataGridView中的特定列设置默认列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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