如何在gridview中动态添加列 [英] how to add columns dynamically in sql from gridview

查看:63
本文介绍了如何在gridview中动态添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,其中包含1到10之间的数字。
基于用户选择的
将显示相应的下拉列表。

i需要在sql中存储下拉列表值数据库。

我怀疑是否需要在数据库中创建10列,或者我可以根据用户选择在db中动态添加列。

解决方案

不,您只需要2列:

  CREATE   TABLE  UserLangs(UserID  INT ,LangID  INT 



其中:

UserID 是用户的外键(来自MyUsers表的数据)

LangID 是语言的外键(来自Languages表的数据)



结构表之间的关系应如下所示:

 MyUsers U. serLangs语言
-------- --------- ---------
UserID INT(一个< - 很多)UserID | - >一)LangID
......(其他字段)LangID(很多---- | ...(其他字段)


您可以使用以下内容创建列并将其添加到表中

 DataColumn col1 = new DataColumn(Col_Name,typeof(string)); 
Datatable1.Columns.Add(col1);
Datatable1.acceptchanges()





希望它能满足您的需求....


i have a dropdownlist which consists of numbers from 1 to 10.
based on user selection respective dropdownlists will get displayed.
i need to store the dropdownlists values in sql database.
My doubt is whether i need to create 10 columns in the database or can i add columns dynamically in db based on user selection.

解决方案

No, you need only 2 columns:

CREATE TABLE UserLangs (UserID INT, LangID INT)


where:
UserID is a foreign-key for users (data coming from MyUsers table)
LangID is a foreign-key for languages(data coming from Languages table)

The structure and relationships between tables should looks like:

MyUsers                   UserLangs                Languages
--------                  ---------                ---------
UserID INT (one <-- many) UserID          |-->one) LangID 
...(other fields)         LangID (many----|         ... (other fields)


You can create and add columns to table using following

DataColumn col1= new DataColumn("Col_Name", typeof(string));
Datatable1.Columns.Add(col1);
Datatable1.acceptchanges()



Hope its what you are looking for....


这篇关于如何在gridview中动态添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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