I have a gridview
<asp:GridView ID="gvOrders" runat="server">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" SortExpression="Item" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnItemID" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
</Columns>
</asp:GridView>
.............................................
Now i want Add Rows to gridview with data getting from Textbox/DropdownList.
And then how to access this GridData rows.
Assuming your GridView datasource is a DataTable named Dtable;
DataRow row = new DataRow();
row["columnName"] = textBox1.Text;
DTable.Rows.Add(row);
gvOrders.DataSource=Dtable;
gvOrders.DataBind();
Please try this, how it will help you.
Aspx Page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
<asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number"></asp:BoundField>
</Columns>
</asp:GridView>
C# Code:
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
}
else
{
dt = (DataTable)ViewState["DataTable"];
}
ViewState["DataTable"] = dt;
}
private void MakeDataTable()
{
dt.Columns.Add("Name");
dt.Columns.Add("Number");
}
protected void Button2_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr["Name"] = txtName.Text;
dr["Number"] = txtNumber.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
Related
I tried many work around but unable to set the column as hyperlink. This is the output i get.
Output that i get
I want to add the hyperlink as below screenshot:
Output with hyperlink
Click on that link able to show the detail as below
This is my code but i unable to add hyperlink:
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat = "server" AutoGenerateColumns="false" OnDataBound="OnDataBound" GridLines="Both">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="150" />
<asp:BoundField DataField="LOT" HeaderText="LOT" ItemStyle-Width="150" />
<asp:BoundField DataField="TYPE" HeaderText="TYPE" ItemStyle-Width="150" />
<asp:BoundField DataField="KEY" HeaderText="KEY" ItemStyle-Width="150" />
<asp:BoundField DataField="VALUE" HeaderText="VALUE" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Code behind that i have done:
protected void btn_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DateTime StartDate = Convert.ToDateTime(datepickerstart.Text);
string Date = StartDate.ToString("dd-MMM-yyyy");
DateTime EndDate = Convert.ToDateTime(datepicker.Text);
string dt2 = EndDate.ToString("dd-MMM-yyyy");
GridView1.DataSource = GetData("Oracle query");
GridView1.DataBind();
txtDate.Text = "You Selected Start:" + Date;
txtDate1.Text = "You Selected End:" + EndDate;
}
private DataTable GetData(string query)
{
string oradb = "Data Source=(DESCRIPTION =" +
"(ADDRESS = (PROTOCOL=TCP)(HOST = '')(PORT = ''))" +
"(CONNECT_DATA =" +
"(SERVER = DEDICATED)" +
"(SERVICE_NAME = '')" +
")" +
");User ID='';Password='';";
DataTable dt = new DataTable();
OracleConnection conn = new OracleConnection(oradb);
using (OracleConnection con = new OracleConnection(oradb))
{
using (OracleCommand cmd = new OracleCommand(query))
{
using (OracleDataAdapter sda = new OracleDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
protected void OnDataBound(object sender, EventArgs e)
{
for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = GridView1.Rows[rowIndex];
GridViewRow previousRow = GridView1.Rows[rowIndex + 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text)
{
row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 :
previousRow.Cells[0].RowSpan + 1;
previousRow.Cells[0].Visible = false;
}
}
}
}
Instead of Bound Field you need to use TemplateField > ItemTemplate > LinkButton just i have used below:
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat = "server" AutoGenerateColumns="false" OnDataBound="OnDataBound" GridLines="Both">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnIdLink" runat="server" Text='<%# Bind("ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LOT" HeaderText="LOT" ItemStyle-Width="150" />
<asp:BoundField DataField="TYPE" HeaderText="TYPE" ItemStyle-Width="150" />
<asp:BoundField DataField="KEY" HeaderText="KEY" ItemStyle-Width="150" />
<asp:BoundField DataField="VALUE" HeaderText="VALUE" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
I have to bind drop down list inside a grid view. While using the given below code showing one error. Help me to find a proper solution. Thank you.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType = DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList DropdownList1 = (DropDownList)e.Row.FindControl("DropdownList1");
ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter at;
at = new ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter();
DataTable dt = new DataTable();
dt=at.GetUpdateTeam();
DropdownList1.DataSource = dt;
DropdownList1.DataTextField = "Assigned_Team";
DropdownList1.DataValueField = "Assigned_Team";
DropdownList1.DataBind();
DropdownList1.Items.FindByValue((e.Row.FindControl("Label1") as Label).Text).Selected = true;
}
}
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="State" />
<asp:BoundField DataField="District" HeaderText="District" ReadOnly="True" SortExpression="District" />
<asp:BoundField DataField="Site_ID" HeaderText="Site ID" ReadOnly="True" SortExpression="Site_ID" />
<asp:BoundField DataField="Site_Name" HeaderText="Site Name" ReadOnly="True" SortExpression="Site_Name" />
<asp:TemplateField HeaderText="Team" SortExpression="Assigned_Team">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Width="88px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Assigned_Team") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Created_Date" HeaderText="Created Date" ReadOnly="True" SortExpression="Created_Date" />
<asp:BoundField DataField="Created_By" HeaderText="Created By" ReadOnly="True" SortExpression="Created_By" />
</Columns>
</asp:GridView>
I believe you have a typo in your if statement.
if (e.Row.RowType = DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
Should be
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
Notice you wrote your first equals as an assignment statement but it should be a conditional equals.
You can do in this way:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList DropdownList1 = (DropDownList)e.Row.FindControl("DropdownList1");
ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter at;
at = new ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter();
DataTable dt = new DataTable();
dt=at.GetUpdateTeam();
DropdownList1.DataSource = dt;
DropdownList1.DataTextField = "Assigned_Team";
DropdownList1.DataValueField = "Assigned_Team";
DropdownList1.DataBind();
DropdownList1.Items.FindByValue((e.Row.FindControl("Label1") as Label).Text).Selected = true;
}
}
I have created one gridview with custom column and want to perform sorting on them. When I click on column then no event is firing for sorting.
Below is the code which I have written
For .aspx page
<asp:GridView ID="grdConfigureCustomers" runat="server" AlternatingRowStyle- BackColor="Aqua" Width="1300px"
OnRowCommand="grdConfigureCustomers_RowCommand" OnRowEditing="grdConfigureCustomers_RowEditing"
OnRowUpdating="grdConfigureCustomers_RowUpdating" OnRowCancelingEdit="grdConfigureCustomers_RowCancelingEdit"
AutoGenerateColumns="false" AllowPaging="true" PageSize="5"
OnRowDeleting="grdConfigureCustomers_RowDeleting" ShowFooter="true"
onpageindexchanging="grdConfigureCustomers_PageIndexChanging" OnSorting="grdConfigureCustomers_Sorting" AllowSorting="true">
<HeaderStyle BackColor="AliceBlue" />
<Columns>
<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Customer_ID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddCustomerId" runat="server"></asp:TextBox><span style="color:Red;">*</span>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For .aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadCustomerConfigurationGridView();
}
}
private void LoadCustomerConfigurationGridView()
{
DataTable dt = new DataTable();
dt = sqlHelper.GetCustomerListInformation();
grdConfigureCustomers.DataSource = dt;
grdConfigureCustomers.DataBind();
Session["data"] = dt;
}
private void LoadCustomerConfigurationGridView(string srtexpr, string direc)
{
DataTable dt = new DataTable();
dt = sqlHelper.GetCustomerListInformation();
DataView dv = new DataView(dt);
dv.Sort = srtexpr + " " + direc;
grdConfigureCustomers.DataSource = dv;
grdConfigureCustomers.DataBind();
Session["data"] = dt;
}
protected void grdConfigureCustomers_Sorting(object sender, GridViewSortEventArgs e)
{
switch (e.SortExpression)
{
case "DateLogged":
if (e.SortDirection == SortDirection.Ascending)
{
LoadCustomerConfigurationGridView("DateLogged", "ASC");
}
else
{
LoadCustomerConfigurationGridView();
}
break;
}
}
Please let me know what I missed for sorting. Thanks in advance.
You need to specify the SortExpression property in the TemplateField like this:
<asp:TemplateField HeaderText="Customer ID" SortExpression="Customer_ID">
You need to enable sorting. EnableSorting="true" in your asp.net:Gridview.
I have aspx code of grid view as follows..
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataKeyNames="Machine no." EnableModelValidation="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Machine no." HeaderText="Machine no."
ReadOnly="True" SortExpression="Machine no." />
<asp:BoundField DataField="Machine Name" HeaderText="Machine Name"
SortExpression="Machine Name" />
<asp:TemplateField HeaderText="Is active?" SortExpression="Is active?">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("[Is active?]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("[Is active?]") %>'>
<asp:ListItem Value="0">Inactive</asp:ListItem>
<asp:ListItem Value="1">Active</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
</asp:GridView>
I have defined the logic in code behind as follows..
CodeBehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindgrid();
}
}
void bindgrid()
{
string q = "SELECT machineno AS 'Machine no.',machinename AS 'Machine Name',active AS 'Is active?' FROM machinedetails";
DataTable dt = dtu.table(q, out error);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindgrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string mno = e.Keys[0].ToString();
string mname = e.NewValues[0].ToString();
string active = e.NewValues[1].ToString();
string q = "update machinedetails set machinename = '"+mname+"',active='"+active+"' where machineno =" + mno;
Response.Write(q);
e.Cancel=true;
GridView1.EditIndex = -1;
bindgrid();
}
But it throws the error:-
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
can any one help me with this??
Steve --> as per your question i changed my logic as below
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string mno = GridView1.DataKeys[e.RowIndex][0].ToString();
GridView gv = (GridView)sender;
for (int i = 0; i < gv.Columns.Count; i++)
{
DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
}
string mname = e.NewValues[0].ToString();
string active = e.NewValues[1].ToString();
string q = "update machinedetails set machinename = '" + mname + "',active='" + active + "' where machineno =" + mno;
Response.Write(q);
e.Cancel=true;
GridView1.EditIndex = -1;
bindgrid();
}
Now I dont get any error but mname is coming same as datakey
my response.write code produse the following output -->update machinedetails set machinename = '2',active='0' where machineno =2
machinename = '2' should be the value that i have entered in the textbox. Dropdownlist value is comming correct.
I have solved this problem by converting all fields to tempelate fields. But still don't know why this happens.
I think I can explain why you're getting the value of MachineNo in mname.
You're saying:
for (int i = 0; i < gv.Columns.Count; i++)
{
DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
}
Then you say
string mname = e.NewValues[0].ToString();
But what is going into Column[0]? It's Machine No:
<asp:BoundField DataField="Machine no." HeaderText="Machine no." ReadOnly="True" SortExpression="Machine no." />
I have a GridView that is bound to a DataTable, and when you select edit, I can get the values to change. However, one field needs to be a dropdownlist and not a textbox. Here's the code I have so far.
protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)Session["table"];
//Update the values.
GridViewRow row = griditems.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text.ToUpper();
dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text.ToUpper();
dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
//dt.Rows[row.DataItemIndex]["Shipping Method"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
DropDownList cmbType = (DropDownList)griditems.Rows[e.RowIndex].FindControl("Shipping Method");
griditems.EditIndex = -1;
BindData();
}
}
When I uncomment the line for the shipping method to be a textbox, it does just as it says, a textbox, not a dropdownlist. I have tried changing it to a DropDownList with no luck.
In the aspx file:
<asp:GridView ID="griditems" runat="server"
onrowdeleting="griditems_RowDeleting" onrowediting="griditems_RowEditing"
onrowupdating="griditems_RowUpdating" AllowPaging="True" onpageindexchanging="griditems_PageIndexChanging"
onrowcancelingedit="griditems_RowCancelingEdit" ViewStateMode="Enabled" Caption="Order Details"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowdatabound="griditems_RowDataBound" >
<EditRowStyle BackColor="#FF9900" BorderStyle="Double" /></asp:GridView>
and when the table is generated:
public void CreateTable()
{
DataTable table = new DataTable();
if (Session["table"] != null)
table = (DataTable)Session["table"];
else
{
table.Columns.Add("Part", typeof(string));
table.Columns.Add("Quantity", typeof(Int32));
table.Columns.Add("Ship-To", typeof(string));
table.Columns.Add("Requested Date", typeof(string));
table.Columns.Add("Shipping Method", typeof(string));
}
DataRow row = table.NewRow();
row["Part"] = part;
row["Quantity"] = qty;
row["Ship-To"] = shipto;
row["Requested Date"] = reqdate;
row["Shipping Method"] = shipmthd;
table.Rows.Add(row);
Session["table"] = table;
griditems.DataSource = table.DefaultView;
griditems.DataBind();
}
Try dis:
Default.aspx: (Best practise use Template Field in GridView)
<asp:GridView ID="gvshipping" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="gvshipping_RowCancelingEdit"
onrowdatabound="gvshipping_RowDataBound"
onrowediting="gvshipping_RowEditing" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Part">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("part") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Bind("part") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quatity">
<ItemTemplate>
<asp:Label ID="lblqty" runat="server" Text='<%# Bind("quantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtqty" runat="server" Text='<%# Bind("quantity") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ship to">
<ItemTemplate>
<asp:Label ID="lblshipto" runat="server" Text='<%# Bind("ShipTo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtshipto" runat="server" Text='<%# Bind("ShipTo") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Post date">
<ItemTemplate>
<asp:Label ID="lblpostdate" runat="server" Text='<%# Bind("RequestedDate")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpostdate" Text='<%# Bind("RequestedDate")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shipping Method">
<ItemTemplate>
<asp:Label ID="lblshipmethod" runat="server" Text='<%# Bind("ShippingMethod")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
gvBind();
}
}
public void gvBind()
{
DataTable dt = createTable();
Session["tb"] = dt;
gvshipping.DataSource = Session["tb"];
gvshipping.DataBind();
}
protected void gvshipping_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dpshipmethod = (DropDownList)e.Row.FindControl("DropDownList1");
//bind dropdownlist
DataTable dt = shipingmethodTable();
dpshipmethod.DataSource = dt;
dpshipmethod.DataTextField = "ShippingMethod";
dpshipmethod.DataValueField = "Id";
dpshipmethod.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
dpshipmethod.SelectedItem.Text = dr["ShippingMethod"].ToString();
}
}
}
protected void gvshipping_RowEditing(object sender, GridViewEditEventArgs e)
{
gvshipping.EditIndex = e.NewEditIndex;
vBind();
}
protected void gvshipping_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvshipping.EditIndex = -1;
gvBind();
}
public DataTable createTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Part", typeof(string));
dt.Columns.Add("Quantity", typeof(Int32));
dt.Columns.Add("ShipTo", typeof(string));
dt.Columns.Add("RequestedDate", typeof(string));
dt.Columns.Add("ShippingMethod", typeof(string));
string date= DateTime.Now.ToShortDateString();
DataRow row = dt.NewRow();
row["Part"] = "Anchor";
row["Quantity"] = "10";
row["ShipTo"] = "blah";
row["RequestedDate"] = date;
row["ShippingMethod"] = "Charge by subtotal";
dt.Rows.Add(row);
DataRow row1 = dt.NewRow();
row1["Part"] = "blade";
row1["Quantity"] = "88";
row1["ShipTo"] = "blah";
row1["RequestedDate"] = date;
row1["ShippingMethod"] = "Charge by quantity";
dt.Rows.Add(row1);
DataRow row2 = dt.NewRow();
row2["Part"] = "cabin";
row2["Quantity"] = "4";
row2["ShipTo"] = "blah";
row2["RequestedDate"] = date;
row2["ShippingMethod"] = "Charge by subtotal";
dt.Rows.Add(row2);
DataRow row3 = dt.NewRow();
row3["Part"] = "cockpit";
row3["Quantity"] = "11";
row3["ShipTo"] = "blah";
row3["RequestedDate"] = date;
row3["ShippingMethod"] = "Charge by weight";
dt.Rows.Add(row3);
DataRow row4 = dt.NewRow();
row4["Part"] = "jack";
row4["Quantity"] = "45";
row4["ShipTo"] = "blah";
row4["RequestedDate"] = date;
row4["ShippingMethod"] = "Charge by weight";
dt.Rows.Add(row4);
DataRow row5 = dt.NewRow();
row5["Part"] = "cabin";
row5["Quantity"] = "67";
row5["ShipTo"] = "blah";
row5["RequestedDate"] = date;
row5["ShippingMethod"] = "Charge by weight";
dt.Rows.Add(row5);
DataRow row6 = dt.NewRow();
row6["Part"] = "blade";
row6["Quantity"] = "4";
row6["ShipTo"] = "blah";
row6["RequestedDate"] = date;
row6["ShippingMethod"] = "Charge by weight";
dt.Rows.Add(row6);
return dt;
}
public DataTable shipingmethodTable()
{
DataTable dtshipingmethod = new DataTable();
dtshipingmethod.Columns.Add("Id", typeof(Int32));
dtshipingmethod.Columns.Add("ShippingMethod", typeof(string));
DataRow row = dtshipingmethod.NewRow();
row["Id"] = 1;
row["ShippingMethod"] = "Charge by subtotal";
dtshipingmethod.Rows.Add(row);
DataRow row1 = dtshipingmethod.NewRow();
row1["Id"] = 2;
row1["ShippingMethod"] = "Charge by weight";
dtshipingmethod.Rows.Add(row1);
DataRow row2 = dtshipingmethod.NewRow();
row2["Id"] = 3;
row2["ShippingMethod"] = "Charge by quantity";
dtshipingmethod.Rows.Add(row2);
return dtshipingmethod;
}
ScreenShot:
you can go ahead with the help of this:
dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)row.FindControl("DropdownList1")).SelectedItem;
EDIT:
dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)
(row.Cells[5].Controls[0])).SelectedItem.ToString();