问题描述
嗨!我在ASP.NET 4.0的Gridview中使用Dropdownlist.在一列上.我在网格上有选择项,但无法将下拉列表中的选定值设置为另一列.以及在下拉列表中选择后如何获取selectedvalue的详细信息.怎么办?
如果有任何解决方案,我会提供问题的详细信息..
辅助表样本数据
Hi ! i''m using Dropdownlist inside Gridview on ASP.NET 4.0. on one column. I have select item on grid but unable to sset dropdownlists selected value to another column. and how to get selectedvalue''s details after selected in dropdownlist. How can be done??
I''m providing problems details if any solution please ..
assigining table Sample data
DataTable dt = (DataTable)Session["TestData"];
if (dt == null)
{
// Create a DataTable and save it to session
//dt = new DataTable();
//dt.Columns.Add(new DataColumn("Id", typeof(int)));
//dt.Columns.Add(new DataColumn("AssignedTo", typeof(string)));
//dt.Columns.Add(new DataColumn("Description", typeof(string)));
//dt.Columns.Add(new DataColumn("Status", typeof(string)));
//dt.Columns.Add(new DataColumn("Tick", typeof(string)));
dt = new DataTable();
dt.Columns.Add(new DataColumn("SN", typeof(int)));
dt.Columns.Add(new DataColumn("ItemID", typeof(Int16)));
dt.Columns.Add(new DataColumn("Product", typeof(string)));
dt.Columns.Add(new DataColumn("Rate", typeof(double)));
dt.Columns.Add(new DataColumn("Quantity", typeof(Int16)));
dt.Columns.Add(new DataColumn("Amount", typeof(float)));
dt.Columns.Add(new DataColumn("Tick", typeof(bool)));
//dt.Rows.Add(new object[] { 1, "Create a new project", "Declan", "Complete", true });
//dt.Rows.Add(new object[] { 2, "Build a demo applcation", "Olive", "In Progress", false });
//dt.Rows.Add(new object[] { 3, "Test the demo applcation", "Peter", "Pending", true });
//dt.Rows.Add(new object[] { 4, "Deploy the demo applcation", "Andy", "Pending", false });
dt.Rows.Add(new object[] { 1, 0, 0, 0, true });
// Add the id column as a primary key
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns["SN"];
dt.PrimaryKey = keys;
_sampleData = dt;
它正在将数据绑定到Gridview
It was binding data into Gridview
_sampleData = null;
GridView1.DataSource = _sampleData;
GridView1.DataBind();
现在设计页面
Now Design Page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E9ECEF" BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Vertical"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
OnRowUpdating="GridView1_RowUpdating" ShowFooter="True" Width="822px"
onrowediting="GridView1_RowEditing">
<Columns>
<asp:ButtonField CommandName="SingleClick" Text="SingleClick" Visible="False" />
<asp:TemplateField HeaderText="SN">
<ItemTemplate>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("SN") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemID" Visible="false">
<ItemTemplate>
<asp:Label ID="IdItem" runat="server" Text='<%# Eval("ItemID") %>'></asp:Label>
<asp:TextBox ID="ItemIDtext" visible="false" runat="server" Text='<%# Eval("ItemID") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="0px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label Width="150px" ID="AssignedToLabel" runat="server" Text='<%# Eval("Product") %>'></asp:Label>
<asp:DropDownList ID="AssignedTo" runat="server" CssClass="gridDown" DataTextField="ItemName" DataValueField="ItemID" DataSourceID="SqlDataSource4" AutoPostBack="true" CausesValidation="true" Visible="false">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Andy</asp:ListItem>
<asp:ListItem>Betty</asp:ListItem>
<asp:ListItem>Conor</asp:ListItem>
<asp:ListItem>Declan</asp:ListItem>
<asp:ListItem>Eamon</asp:ListItem>
<asp:ListItem>Fergal</asp:ListItem>
<asp:ListItem>Gordon</asp:ListItem>
<asp:ListItem>Helen</asp:ListItem>
<asp:ListItem>Iris</asp:ListItem>
<asp:ListItem>John</asp:ListItem>
<asp:ListItem>Kevin</asp:ListItem>
<asp:ListItem>Lorna</asp:ListItem>
<asp:ListItem>Matt</asp:ListItem>
<asp:ListItem>Nora</asp:ListItem>
<asp:ListItem>Olive</asp:ListItem>
<asp:ListItem>Peter</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Rate") %>'></asp:Label>
<asp:TextBox ID="Description" runat="server" Text='<%# Eval("Rate") %>'
visible="false"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" >
<ItemTemplate>
<asp:Label ID="StatusLabel" runat="server"
Text='<%# Eval("Quantity") %>'></asp:Label>
<asp:TextBox ID="Status" runat="server" Text='<%# Eval("Quantity") %>'
visible="false"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="AmountsLabel" runat="server"
Text='<%# Eval("Amount") %>'></asp:Label>
<asp:TextBox ID="AountStatus" runat="server" Text='<%# Eval("Amount") %>'
visible="false"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Tick" Visible="false">
<ItemTemplate>
<asp:Label ID="TickLabel" runat="server" Text='<%# Eval("Tick") %>'></asp:Label>
<asp:CheckBox ID="Tick" runat="server" AutoPostBack="true" Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerStyle" ForeColor="White" />
<RowStyle CssClass="rowStyle" />
<AlternatingRowStyle CssClass="alternatingRowStyle" />
<FooterStyle CssClass="footerStyle" />
<PagerStyle CssClass="pagerStyle" ForeColor="White" />
</asp:GridView>
<asp:LinkButton ID="AddRow" runat="server" Font-Bold="True"
OnClick="AddRow_Click" Text="Add Row"></asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:constr %>"
SelectCommand="SELECT ItemID ,ItemName,PurchaseRate FROM ItemDetails">
</asp:SqlDataSource>
Row Command
Row Command
GridView _gridView = (GridView)sender;
switch (e.CommandName)
{
case ("SingleClick"):
// Get the row index
int _rowIndex = int.Parse(e.CommandArgument.ToString());
// Parse the event argument (added in RowDataBound) to get the selected column index
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
// Set the Gridview selected index
_gridView.SelectedIndex = _rowIndex;
// Bind the Gridview
_gridView.DataSource = _sampleData;
_gridView.DataBind();
// Write out a history if the event
//this.Message.Text += "Single clicked GridView row at index " + _rowIndex.ToString()
// + " on column index " + _columnIndex + "<br />";
// Get the display control for the selected cell and make it invisible
Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = false;
// Get the edit control for the selected cell and make it visible
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;
// Clear the attributes from the selected cell to remove the click event
_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear();
// Set focus on the selected edit control
ScriptManager.RegisterStartupScript(this, GetType(), "SetFocus",
"document.getElementById('" + _editControl.ClientID + "').focus();", true);
// If the edit control is a dropdownlist set the
// SelectedValue to the value of the display control
if (_editControl is DropDownList && _displayControl is Label)
{
((DropDownList)_editControl).SelectedValue = ((Label)_displayControl).Text;
}
// If the edit control is a textbox then select the text
if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
}
// If the edit control is a checkbox set the
// Checked value to the value of the display control
if (_editControl is CheckBox && _displayControl is Label)
{
((CheckBox)_editControl).Checked = bool.Parse(((Label)_displayControl).Text);
}
//GridViewSetFocus(GridView1.EditIndex);
break;
现在
Updatin Row
Now
Updatin Row
GridView _gridView = (GridView)sender;
if (e.RowIndex > -1)
{
// Loop though the columns to find a cell in edit mode
for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++)
{
// Get the editing control for the cell
Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[3];
if (_editControl.Visible)
{
int _dataTableColumnIndex = i - 1;
try
{
// Get the id of the row
Label idLabel = (Label)_gridView.Rows[e.RowIndex].FindControl("IdLabel");
int id = int.Parse(idLabel.Text);
// Get the value of the edit control and update the DataTable
DataTable dt = _sampleData;
DataRow dr = dt.Rows.Find(id);
dr.BeginEdit();
if (_editControl is TextBox)
{
dr[_dataTableColumnIndex] = ((TextBox)_editControl).Text;
}
else if (_editControl is DropDownList)
{
dr[_dataTableColumnIndex] = ((DropDownList)_editControl).SelectedValue;//.SelectedItem.Text;
//ItemID = ((DropDownList)_editControl).SelectedValue;
}
else if (_editControl is CheckBox)
{
dr[_dataTableColumnIndex] = ((CheckBox)_editControl).Checked;
}
dr.EndEdit();
// Save the updated DataTable
_sampleData = dt;
// Clear the selected index to prevent
// another update on the next postback
_gridView.SelectedIndex = -1;
// Repopulate the GridView
_gridView.DataSource = dt;
_gridView.DataBind();
}
catch (ArgumentException)
{
//this.Message.Text += "Error updating GridView row at index "
// + e.RowIndex + "<br />";
// Repopulate the GridView
_gridView.DataSource = _sampleData;
_gridView.DataBind();
}
}
}
}
推荐答案
Row Command
Row Command
GridView _gridView = (GridView)sender;
switch (e.CommandName)
{
case ("SingleClick"):
// Get the row index
int _rowIndex = int.Parse(e.CommandArgument.ToString());
// Parse the event argument (added in RowDataBound) to get the selected column index
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
// Set the Gridview selected index
_gridView.SelectedIndex = _rowIndex;
// Bind the Gridview
_gridView.DataSource = _sampleData;
_gridView.DataBind();
// Write out a history if the event
//this.Message.Text += "Single clicked GridView row at index " + _rowIndex.ToString()
// + " on column index " + _columnIndex + "<br />";
// Get the display control for the selected cell and make it invisible
Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = false;
// Get the edit control for the selected cell and make it visible
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;
// Clear the attributes from the selected cell to remove the click event
_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear();
// Set focus on the selected edit control
ScriptManager.RegisterStartupScript(this, GetType(), "SetFocus",
"document.getElementById('" + _editControl.ClientID + "').focus();", true);
// If the edit control is a dropdownlist set the
// SelectedValue to the value of the display control
if (_editControl is DropDownList && _displayControl is Label)
{
((DropDownList)_editControl).SelectedValue = ((Label)_displayControl).Text;
}
// If the edit control is a textbox then select the text
if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
}
// If the edit control is a checkbox set the
// Checked value to the value of the display control
if (_editControl is CheckBox && _displayControl is Label)
{
((CheckBox)_editControl).Checked = bool.Parse(((Label)_displayControl).Text);
}
//GridViewSetFocus(GridView1.EditIndex);
break;
现在
Updatin Row
Now
Updatin Row
GridView _gridView = (GridView)sender;
if (e.RowIndex > -1)
{
// Loop though the columns to find a cell in edit mode
for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++)
{
// Get the editing control for the cell
Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[3];
if (_editControl.Visible)
{
int _dataTableColumnIndex = i - 1;
try
{
// Get the id of the row
Label idLabel = (Label)_gridView.Rows[e.RowIndex].FindControl("IdLabel");
int id = int.Parse(idLabel.Text);
// Get the value of the edit control and update the DataTable
DataTable dt = _sampleData;
DataRow dr = dt.Rows.Find(id);
dr.BeginEdit();
if (_editControl is TextBox)
{
dr[_dataTableColumnIndex] = ((TextBox)_editControl).Text;
}
else if (_editControl is DropDownList)
{
dr[_dataTableColumnIndex] = ((DropDownList)_editControl).SelectedValue;//.SelectedItem.Text;
//ItemID = ((DropDownList)_editControl).SelectedValue;
}
else if (_editControl is CheckBox)
{
dr[_dataTableColumnIndex] = ((CheckBox)_editControl).Checked;
}
dr.EndEdit();
// Save the updated DataTable
_sampleData = dt;
// Clear the selected index to prevent
// another update on the next postback
_gridView.SelectedIndex = -1;
// Repopulate the GridView
_gridView.DataSource = dt;
_gridView.DataBind();
}
catch (ArgumentException)
{
//this.Message.Text += "Error updating GridView row at index "
// + e.RowIndex + "<br />";
// Repopulate the GridView
_gridView.DataSource = _sampleData;
_gridView.DataBind();
}
}
}
}
这篇关于在Gridview内的另一列中设置下拉列表的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!