【问题标题】:Grid view Row Data Bound putting data in a particular cell网格视图行数据绑定将数据放入特定单元格
【发布时间】:2013-11-29 15:46:49
【问题描述】:

我有 3 张桌子 CustomerAddressMatching_Customer_Address

CustomerName 列,AddressCity 列。他们还有其他一些专栏,但我不需要它们。 NameCity 列包含此类数据。

   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


【解决方案1】:

将查找匹配项的逻辑放入您发布的数据表构建逻辑中,如下所示:

// This loop is getting rows from table Customer and adding them as Rows of dt.
for (int x = 0; x < (lengthofCustomer); x++)
{
    // Build the pieces of data for your row here

    // Name

    // Loop through each city
    for (int y = 0; y < (lengthofAddress); y++)
    {
        // Determine if each city is a match or not,
        // if so then put "X" in that row's cell here

    }
}

现在,当您将网格与数据表绑定时,您将不需要处理 RowDataBound 事件,因为 X 已经在正确的单元格中了。


更新:

要将值放入新的数据表行,您需要创建一个新行,然后通过该行的索引应用单元格值,如下所示:

DataRow row;

// Create new DataRow objects and add to DataTable.     
for(int i = 0; i < 10; i++)
{
    row = YourDataTable.NewRow();

    row["Name"] = theName;

    // Loop through each city
    for (int y = 0; y < (lengthofAddress); y++)
    {
        // Determine if each city is a match or not,
        // if so then put "X" in that row's cell here
        if(match)
        {
            row[y+1] = "X";
        }            
    }

    YourDataTable.Rows.Add(row);
}

更新 2:

如果匹配列中每个人的行已经存在,则循环遍历每一行,如下所示:

foreach(DataRow row in TheTable.Rows)
{
    // Loop through each city
    for (int y = 0; y < (lengthofAddress); y++)
    {
        // Determine if each city is a match or not,
        // if so then put "X" in that row's cell here
        if(match)
        {
            row[y+1] = "X";
        }            
    }
}

【讨论】:

  • @Cami - 您可以在此放置使用 Matching_Customer_Address 表中的 ID 的逻辑来检查当前行的名称和每个城市,从而循环遍历城市名称。跨度>
  • 在内部循环中,您给出了注释“// 如果是,则将“X”放在该行的单元格中”。我知道我可以在 dt 中添加一行如何访问 dt 的单元格。
  • Andersson 我已经检查了您的代码,它将 X 放入单元格中,但它在最后向数据表中添加了新行。
  • 哦,所以你已经为匹配列中的每个人创建了行?我已经用另一个更新更新了答案,以说明已经存在的行。
  • @Cami - 请参阅UPDATE 2 的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多