GridView控件在ASP.Net - 选择正确的行 [英] GridView in ASP.Net -- Selecting the correct row

查看:131
本文介绍了GridView控件在ASP.Net - 选择正确的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包括在它的GridView。这是GridView的分页与10个项目在一个时间。通常情况下,我希望用户选择在GridView的项目,并移植FormView。这个效果很好。

I have a page that includes a GridView in it. That GridView is paged with 10 items at a time. Normally, I want the user to select the item from the GridView and populate the FormView. This works well.

我也想支持的查询参数?ID = n,其中的页面将加载指定的项目。

I also want to support a query parameter ?ID=n where the page will load the specified item.

我如何告诉DataGrid中或项目设置为数据上下文中的数据源?

How do I tell the DataGrid or the data source which item to set as the data context?

我想在DataGrid转到正确的页面并选择该项目,显示FormView控件指定的项目。

I want the DataGrid to go to the proper page and select the item, showing the specified item in the FormView.

我无法弄清楚如何比数据源限制到具体项目,这是混淆用户做其他。

I can't figure out how to do this other than limiting the data source to the specific item, which is confusing to the user.

有什么想法?

推荐答案

如果您设置在GridView的DataKey领域包含主键,有<一个href=\"http://www.$c$cproject.com/KB/webforms/GridViewSelector.aspx?fid=1014498&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2804567\">this $ C如何设置一个GridView的选择指标的基础上,记录的键值,使用扩展方法$的CProject文章:

If you set the DataKey field of the GridView to contain the primary key, there is this CodeProject article on how to set the selected index of a gridview, based on the key value of the record, using an extension method:

public static void SetRowValueValueByKey(this GridView GridView, string DataKeyValue)
{
    int intSelectedIndex = 0;
    int intPageIndex = 0;
    int intGridViewPages = GridView.PageCount;

    // Loop thru each page in the GridView
    for (int intPage = 0; intPage < intGridViewPages; intPage++)
    {
    	// Set the current GridView page
    	GridView.PageIndex = intPage;
    	// Bind the GridView to the current page
    	GridView.DataBind();
    	// Loop thru each DataKey in the GridView
    	for (int i = 0; i < GridView.DataKeys.Count; i++)
    	{
    		if (Convert.ToString(GridView.DataKeys[i].Value) == DataKeyValue)
    		{
    			// If it is a match set the variables and exit
    			intSelectedIndex = i;
    			intPageIndex = intPage;
    			break;
    		}
    	}
    }

    // Set the GridView to the values found
    GridView.PageIndex = intPageIndex;
    GridView.SelectedIndex = intSelectedIndex;
    GridView.DataBind();
}

这篇关于GridView控件在ASP.Net - 选择正确的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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