Firstly, it is not realy possible for anyone to give any useful suggestions at the moment, as you have not indicated which line is causing the error.
Secondly, I'm not clear why you are dynamically generating tables within your GridView. If you need to generate another table within your gridview cell, why not put a repeater, listview, or even another nested GridView into your cell template? You can then
bind the child control in your GridView RowDataBound event handler- and this will be a lot neater than generating dynamic controls.
I think lblvoucherno is Label and you are trying to convert into LinkButton, this is giving error. Why you want to convert into LinkButton...change into label
neha0123
Member
50 Points
224 Posts
gridview row data bound..
Sep 08, 2009 12:31 PM|LINK
I am using Row data to bind data in gridview when i use
selectindex then there is an error object is not instance of the object
How to use row databound selected index changed
Plz Suggest Me
My row data bound event is this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
double prevPrice = 0.0;
switch (gvr.RowType)
{
case DataControlRowType.DataRow:
{
DataRowView drv = gvr.DataItem as DataRowView;
string subcategory = drv["PartyName"].ToString();
string openingbalance = drv["Opening_Blnc"].ToString();
double opbalance = Convert.ToDouble(openingbalance);
if (!subcategory.Equals(m_subcategory))
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.BackColor = Color.DarkRed;
gridcolor = Color.DarkRed;
cell.ForeColor = Color.White;
cell.ColumnSpan = 5;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Text = subcategory;
row.Cells.Add(cell);
TableCell ce = new TableCell();
ce.BackColor = Color.DarkRed;
gridcolor = Color.DarkRed;
ce.ForeColor = Color.White;
ce.Text = "Opening Balance";
row.Cells.Add(ce);
TableCell ce1 = new TableCell();
ce1.BackColor = Color.DarkRed;
gridcolor = Color.DarkRed;
ce1.ForeColor = Color.White;
ce1.HorizontalAlign = HorizontalAlign.Right;
ce1.Text = opbalance.ToString("0.00");
row.Cells.Add(ce1);
sum1 = sum1 + row.Cells.Count;
GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count - 1, row);
// GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count +1, row);
m_subcategory = subcategory;
// GridViewRow Rowsparty = this.GridView1.Rows[e.Row.RowIndex];
// GridViewRow Rowsparty1 = this.GridView1.Rows[e.Row.RowIndex-1];
}
else
{
gridcolor = Color.Black;
}
if(gridcolor==Color.DarkRed)
{
//if (!subcategory.Equals(m_subcategory))
// {
double length = Convert.ToDouble(((Label)e.Row.FindControl("Label5")).Text);
double width = Convert.ToDouble(((Label)e.Row.FindControl("Label6")).Text);
// Double sum = Convert.ToDouble(prev.Text) + length + width;
Double sum = opbalance-length + width;
Label str = ((Label)e.Row.FindControl("Label12"));
str.ToolTip = sum.ToString("0.00");
if (sum > 0)
{
str.Text= sum.ToString("0.00") + " " + "Cr";
}
else
{
sum = sum * (-1);
str.Text = sum.ToString("0.00") + " " + "Dr";
}
}
else
{
if ((e.Row.RowIndex > 0) && (subcategory.Equals(m_subcategory)))
{
//// Label drcrs = (Label)GridView1.Rows[e.Row.RowIndex - 1].Cells[7].FindControl("Label4");
GridViewRow prevRow = this.GridView1.Rows[e.Row.RowIndex - 1];
if (null != prevRow && prevRow.RowType == DataControlRowType.DataRow)
{
double.TryParse(prevRow.Cells[7].ToolTip, out prevPrice);
}
Label prev = (Label)GridView1.Rows[e.Row.RowIndex - 1].Cells[7].FindControl("Label12");
double length = Convert.ToDouble(((Label)e.Row.FindControl("Label5")).Text);
double width = Convert.ToDouble(((Label)e.Row.FindControl("Label6")).Text);
string drcr = (((Label)e.Row.FindControl("Label4")).Text);
//if (drcr = "Dr")
// {
// }
Double sum = Convert.ToDouble(prev.ToolTip)- length + width;
// Double sum = 0.0;
// sum = sum + length + width;
Label str = ((Label)e.Row.FindControl("Label12"));
str.ToolTip = sum.ToString("0.00");
if (sum > 0)
{
str.Text = sum.ToString("0.00") + " " + "Cr";
}
else
{
sum = sum * (-1);
str.Text = sum.ToString("0.00") + " " + "Dr";
}
}
}
break;
}
}
}
Plz Help Me
booler
Star
14342 Points
2205 Posts
Re: gridview row data bound..
Sep 08, 2009 01:18 PM|LINK
Firstly, it is not realy possible for anyone to give any useful suggestions at the moment, as you have not indicated which line is causing the error.
Secondly, I'm not clear why you are dynamically generating tables within your GridView. If you need to generate another table within your gridview cell, why not put a repeater, listview, or even another nested GridView into your cell template? You can then bind the child control in your GridView RowDataBound event handler- and this will be a lot neater than generating dynamic controls.
neha0123
Member
50 Points
224 Posts
Re: gridview row data bound..
Sep 09, 2009 05:04 AM|LINK
Cs File
private string m_subcategory = String.Empty;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
double prevPrice = 0.0;
switch (gvr.RowType)
{
case DataControlRowType.DataRow:
{
DataRowView drv = gvr.DataItem as DataRowView;
string subcategory = drv["PartyName"].ToString();
string openingbalance = drv["Opening_Blnc"].ToString();
double opbalance = Convert.ToDouble(openingbalance);
if (!subcategory.Equals(m_subcategory))
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.BackColor = Color.DarkRed;
gridcolor = Color.DarkRed;
cell.ForeColor = Color.White;
cell.ColumnSpan = 5;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Text = subcategory;
row.Cells.Add(cell);
TableCell ce = new TableCell();
ce.BackColor = Color.DarkRed;
gridcolor = Color.DarkRed;
ce.ForeColor = Color.White;
ce.Text = "Opening Balance";
row.Cells.Add(ce);
TableCell ce1 = new TableCell();
ce1.BackColor = Color.DarkRed;
gridcolor = Color.DarkRed;
ce1.ForeColor = Color.White;
ce1.HorizontalAlign = HorizontalAlign.Right;
ce1.Text = opbalance.ToString("0.00");
row.Cells.Add(ce1);
sum1 = sum1 + row.Cells.Count;
GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count - 1, row);
// GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count +1, row);
m_subcategory = subcategory;
// GridViewRow Rowsparty = this.GridView1.Rows[e.Row.RowIndex];
// GridViewRow Rowsparty1 = this.GridView1.Rows[e.Row.RowIndex-1];
}
else
{
gridcolor = Color.Black;
}
if(gridcolor==Color.DarkRed)
{
//if (!subcategory.Equals(m_subcategory))
// {
double length = Convert.ToDouble(((Label)e.Row.FindControl("Label5")).Text);
double width = Convert.ToDouble(((Label)e.Row.FindControl("Label6")).Text);
// Double sum = Convert.ToDouble(prev.Text) + length + width;
Double sum = opbalance-length + width;
Label str = ((Label)e.Row.FindControl("Label12"));
str.ToolTip = sum.ToString("0.00");
if (sum > 0)
{
str.Text= sum.ToString("0.00") + " " + "Cr";
}
else
{
sum = sum * (-1);
str.Text = sum.ToString("0.00") + " " + "Dr";
}
}
else
{
if ((e.Row.RowIndex > 0) && (subcategory.Equals(m_subcategory)))
{
//// Label drcrs = (Label)GridView1.Rows[e.Row.RowIndex - 1].Cells[7].FindControl("Label4");
GridViewRow prevRow = this.GridView1.Rows[e.Row.RowIndex - 1];
if (null != prevRow && prevRow.RowType == DataControlRowType.DataRow)
{
double.TryParse(prevRow.Cells[7].ToolTip, out prevPrice);
}
Label prev = (Label)GridView1.Rows[e.Row.RowIndex - 1].Cells[7].FindControl("Label12");
double length = Convert.ToDouble(((Label)e.Row.FindControl("Label5")).Text);
double width = Convert.ToDouble(((Label)e.Row.FindControl("Label6")).Text);
string drcr = (((Label)e.Row.FindControl("Label4")).Text);
//if (drcr = "Dr")
// {
// }
Double sum = Convert.ToDouble(prev.ToolTip)- length + width;
// Double sum = 0.0;
// sum = sum + length + width;
Label str = ((Label)e.Row.FindControl("Label12"));
str.ToolTip = sum.ToString("0.00");
if (sum > 0)
{
str.Text = sum.ToString("0.00") + " " + "Cr";
}
else
{
sum = sum * (-1);
str.Text = sum.ToString("0.00") + " " + "Dr";
}
}
}
break;
}
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//Label ss = (Label)(GridView1.Rows[e.NewSelectedIndex].FindControl("lblvoucherno"));
GridViewRow row = GridView1.SelectedRow;
string vouchertype = row.Cells[1].Text;
string voucherno = row.Cells[3].Text;
if (vouchertype == "Sales")
{
Response.Redirect("~/Sales/Sales_Invoice_Entry.aspx?voucherno=" + voucherno + "");
}
else if (vouchertype == "Purchase")
{
Response.Redirect("~/Purchase/Purchase_Entry.aspx?voucherno=" + voucherno + "");
}
else
{
Response.Redirect("Voucher_Entry.aspx?voucherdate=" + voucherno + "");
}
}
.aspx Page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="3"
Font-Size ="X-Small" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" Width="980px"
onrowdatabound="GridView1_RowDataBound" AutoGenerateSelectButton="True"
>
<Columns>
<asp:TemplateField HeaderText="VoucherNo">
<ItemTemplate>
<asp:Label ID="lblvoucherno" runat="server" Text='<%# Eval("Voucherno")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="voucherDate" HeaderText="voucherDate"
HtmlEncode="False" SortExpression="voucherDate" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="VoucherType">
<ItemTemplate>
<asp:Label ID="lblvouchertype" runat="server" Text='<%# Eval("Vouchertype")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DrCr" Visible="False" >
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("ProductName","{0:n}") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DebitAmt" >
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("DebitAmt","{0:n}") %>' ></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreditAmt">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("creditamt","{0:n}") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TotalAmt" Visible="true">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotal" runat="server"
Font-Bold="True" Width="125px"></asp:Label>
</FooterTemplate>
<FooterStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField Visible="False"
>
<ItemTemplate>
<asp:Label ID="lblparty" runat="server" Text='<%# Eval("PartyName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Show Details" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton8" runat="server" CausesValidation="False" CommandName="Select"
Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="Red" Font-Bold="True"
Font-Size="Small" />
<RowStyle ForeColor="#330099" BackColor="White" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC"
Font-Size="X-Small" />
</asp:GridView>
I got an error when i select index changed
string vouchertype = row.Cells[1].Text;
Object reference not set to an instance of an object.
manoj0682
Star
8479 Points
1499 Posts
Re: gridview row data bound..
Sep 09, 2009 05:23 AM|LINK
I guess you are calling wrong event I think you should be calling IndexChanged event and not IndexChanging event
OnSelectedIndexChanged="gv_SelectedIndexChanged"
Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
also check below link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events.aspx
Manoj Karkera
neha0123
Member
50 Points
224 Posts
Re: gridview row data bound..
Sep 09, 2009 05:59 AM|LINK
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;//GridViewRow row = GridView1.SelectedRow; string vouchertype = row.Cells[1].Text;
string voucherno = row.Cells[3].Text;//if (vouchertype == "Sales")
}
i have do this but there is no data in row.Cells[1].Text
it select blanks
What I do Plz Suggest Me.
manoj0682
Star
8479 Points
1499 Posts
Re: gridview row data bound..
Sep 09, 2009 06:05 AM|LINK
you are using TemplateColumn so you have to use FindControl. for your label lblvouchertype.
GridViewRow row = GridView1.SelectedRow;
Label lbl = (Label)row.FindControl("lblvouchertype");
now it should work :)
Manoj Karkera
neha0123
Member
50 Points
224 Posts
Re: gridview row data bound..
Sep 09, 2009 07:28 AM|LINK
GridViewRow row = GridView1.SelectedRow;LinkButton voucherno = (LinkButton)row.FindControl("lblvoucherno");if (voucherno.ToolTip == "Sales")
Response.Redirect("~/Sales/Sales_Invoice_Entry.aspx?voucherno=" + voucherno.Text + "");
it can not find first rows
manoj0682
Star
8479 Points
1499 Posts
Re: gridview row data bound..
Sep 09, 2009 08:08 AM|LINK
I think lblvoucherno is Label and you are trying to convert into LinkButton, this is giving error. Why you want to convert into LinkButton...change into label
Manoj Karkera
neha0123
Member
50 Points
224 Posts
Re: gridview row data bound..
Sep 09, 2009 11:33 AM|LINK
this is not a problem i change the linkbutton......
mendhak
All-Star
17868 Points
2787 Posts
Re: gridview row data bound..
Sep 09, 2009 12:26 PM|LINK
Show your gridview's markup please.