我正在尝试从我的带有一些数据的radGrid中插入数据到我的sql。在我的rad网格中我也有一个复选框列,所以我希望在我的数据库中插入已选中的行。我使用下面的代码,但给我这个错误: 错误30' Telerik.Web.UI.RadGrid'不包含'行'的定义没有延伸方法'行'接受类型为#Telerik.Web.UI.RadGrid'的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)任何想法我在哪里做错了???
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT [ReportId], [Report], [IsSelected] FROM Hobbies"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
RadGrid1.DataSource = dt;
RadGrid1.DataBind();
}
}
}
}
}
protected void Rows()
{
}
protected void Save2_Click(object sender,EventArgs e) {
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Rights"].ConnectionString);
string insert = "insert into Rights(Name,Mbiemri,Gjinia,Departamenti,Mosha,IntRights,Transferta,Depozita,Rapore) values (@Name,@Mbiemri,@Gjinia,@Departamenti,@Mosha,@IntRights,@Transferta,@Depozita,@Rapore)";
SqlCommand cnd = new SqlCommand(insert, con);
con.Open();
foreach (GridViewRow row in RadGrid1.Rows)
{
//Get the HobbyId from the DataKey property.
int ReportID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
//Get the checked value of the CheckBox.
bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;
//Save to database
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@ReportId", ReportID);
cmd.Parameters.AddWithValue("@IsSelected", isSelected);
cmd.ExecuteNonQuery();
}
cnd.Parameters.AddWithValue("@Name", TextBox1.Text);
cnd.Parameters.AddWithValue("@Mbiemri", TextBox2.Text);
cnd.Parameters.AddWithValue("@Gjinia", RadioButtonList1.SelectedValue);
cnd.Parameters.AddWithValue("@Departamenti", SelectDepartament.SelectedItem.Text);
cnd.Parameters.AddWithValue("@Mosha", RadDropDownList1.SelectedItem.Text);
cnd.Parameters.AddWithValue("@IntRights", RadDropDownList2.SelectedItem.Text);
cnd.Parameters.AddWithValue("@Transferta", TransfertaBtn.SelectedValue);
cnd.Parameters.AddWithValue("@Depozita", DepoziteBtn.SelectedValue);
cnd.ExecuteNonQuery();
Response.Redirect("Home.aspx");
con.Close();
}
catch (Exception ex)
{
}
telerik:RadGrid ID="RadGrid1"
runat="server" AllowMultiRowSelection="True" AllowPaging="True" DataSourceID="SqlDataSource1" GridLines="Both" PageSize="5" >
<GroupingSettings CollapseAllTooltip="Collapse all groups" />
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
<MasterTableView>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked='<%# Eval("IsSelected") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Report" HeaderText="Report" ItemStyle-Width="150px" />
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks2014ConnectionString %>" SelectCommand="SELECT [ReportID], [Report] FROM [Report]"></asp:SqlDataSource>
<br />
答案 0 :(得分:0)
要访问行,请遍历其DataItems。有关Accessing Cells and Rows
的更多信息protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridDataItem row in RadGrid1.Items)
{
string rowValue = row["ColumnUniqueName"].Text;
}
}
更好的方法是创建一个几乎没有参数来更新SQL数据库的方法。完成后,在RadGrid(Differences Between ItemCreated and ItemDataBound)的ItemDataBound事件中调用它。在这种情况下,您可以访问行,其单元格,获取必要的值并逐行更新数据库。
bool IsClicked = false;
protected void Button1_Click(object sender, EventArgs e)
{
IsClicked = true;
RadGrid1.Rebind();
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (IsClicked && e.Item is GridDataItem)
{
updateDatabase(((GridDataItem)e.Item)["ColumnUniqueName"].Text);
}
}
protected void updateDatabase(string field)
{
// update SQL
}