【发布时间】:2013-11-29 15:46:49
【问题描述】:
我有 3 张桌子 Customer、Address 和 Matching_Customer_Address。
Customer 有 Name 列,Address 有 City 列。他们还有其他一些专栏,但我不需要它们。 Name 和 City 列包含此类数据。
NAME City
---------- -----------
John New York
---------- ----------
Karin Hamburg
--------- ----------
Jona Tokyo
--------- ----------
Martin
Matching_Customer_Address 包含两个表的 ID,它们之间有匹配。
我有一个网格视图,我将此GridView1 与数据表dt 绑定。 dt 是这种格式:
数据表 dt 第一列是“匹配”,其余列来自 Address.City,行来自 Customer.Name。
由于 Gridview1 与 dt 绑定,所以网格视图是这样的。
Match | New York| Hamburg | Tokyo |
----- | --------| ------- | ------ |
John | | | |
----- | ------- | ------- | ------ |
Karin | | | |
----- | ------- | ------- | ------ |
Jona | | | |
----- | ------- | ------- | ------ |
Martin| | | |
----- | ------- | ------- | ------ |
现在通过使用表格Matching_Customer_Address,我想在GridView1 单元格中添加一个字符“X”。我正在使用RowDataBound 事件来执行此任务,但我不知道应该如何进行。
我正在努力
e.Row.Cells[].Text = "X";
我知道如何在此处访问Matching_Customer_Address 表但是如果找到匹配项,我不知道如何在特定单元格中放置 X。我对 C# 很陌生。
这里是用gridview绑定数据的代码。
DataTable dt = new DataTable();
SqlDataAdapter da_Customer, da_Address;
DataSet ds_Customer = new DataSet();
DataSet ds_Address = new DataSet();
SqlConnection con;
con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True");
da_Customer= new SqlDataAdapter("Select Name from Customer ", con);
da_Customer.Fill(ds_Customer, "Name");
da_Address = new SqlDataAdapter("Select City from Address ", con);
da_Address .Fill(ds_Address, "City");
int lengthofAddress = ds_Address.Tables[0].Select("City is not null").Length;
string[] getCols_City = new string[lengthofAddress];
int lengthofCustomer = ds_Customer.Tables[0].Select("Customer is not null").Length;
string[] getRows_Customer = new string[lengthofCustomer];
//added first column of dt.
dt.Columns.Add(new DataColumn("Name", typeof(string)));
// This loop is getting rows from table City and adding them as column of dt.
for (int x = 0; x < (lengthofAddress); x++)
{
string mystring = (ds_Address.Tables[0].Rows[x]["City"].ToString());
getRows_Customer[x] = mystring;
dt.Columns.Add(getRows_Customer[x]);
}
// This loop is getting rows from table Customer and adding them as Rows of dt.
for (int x = 0; x < (lengthofCustomer); x++)
{
getRows_Customer[x] = (ds_Customer.Tables[0].Rows[x]["Name"].ToString());
dt.Rows.Add(getRows_Customer[x]);
}
GridView1.DataSource = dt;
GridView1.DataBind();
Aspx 代码在这里:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderWidth="1px" CellPadding="4" Font-Names="Arial" Font-Size="Small" Width="100%"
BorderStyle="None" OnRowEditing="GridView1_RowEditing" onrowdatabound="GridView1_RowDataBound" >
<HeaderStyle CssClass="GridviewScrollHeader" />
<RowStyle CssClass="GridviewScrollItem" />
<PagerStyle CssClass="GridviewScrollPager" />
</asp:GridView>
【问题讨论】:
-
发布您的代码,包括代码隐藏和标记。
-
@KarlAnderson 我现在已经在我的问题中添加了代码
标签: c# asp.net visual-studio-2012